SMK  v0.0.1-2561
Simple Message Keeper
SMK Class Referencefinal

Class to implement reading, writing, adding, deleting, and formatting messages from the SMK .json file. More...

#include <smk.hpp>

Collaboration diagram for SMK:

Public Member Functions

 SMK (void)
 Constructor. More...
 
 ~SMK (void)
 Destructor. More...
 
json load (void)
 Load the json from filename. More...
 
SMKsave (json &j)
 Save the json to filename. More...
 
SMKshow_usage (const std::string &arg0)
 Show a block of text that describes how to use SMK. More...
 
SMKreset (void)
 Erase the json in filename and start with a brand new json structure. More...
 
SMKclean (void)
 Clean up old entries in filename. Will only keep the last few recent messages. More...
 
SMKadd (const std::string &priority, const std::string &msg, const bool unique=false)
 Add or update a message in filename. More...
 
SMKunique (const std::string &priority, const std::string &msg)
 Same thing as add() but sets the unique parameter to true. More...
 
SMKdel (const std::string &parm)
 Delete a message from the json in filename. More...
 
SMKdump (void)
 Dump the full json in filename to stdout. More...
 
SMKformat (void)
 Format all the message to an easy-to-read block of text. This can be used as a login message. More...
 
SMKget_all (void)
 Get a tab-delimited set of all messages. More...
 
SMKget_recent (void)
 Get a tab-delimited set of recent messages. More...
 
SMKget (const std::string &parm)
 Get a specific message from filename. The parm must be a valid numerical index, starting at 1. More...
 

Protected Member Functions

SMKinitialize_json (json &j)
 Insert a few housekeeping items into the json. More...
 
SMKsort_messages_by_timestamp (json &j)
 Ensure the messages in filename are sorted in order from oldest to newest. More...
 
std::string format (const std::time_t now, const std::time_t tt)
 Format the given timestamp as an easy-to-read text string, such as "35 seconds ago" or "2 days ago". More...
 
std::string format (const int priority)
 Format the numeric priority index to a text string, such as high or low. More...
 
SMKdisplay_msg (const json &j)
 Format the given message or messages as a tab-delimited block of text. More...
 

Protected Attributes

std::string filename
 The location where the json structure is stored. This file must have read/write permissions. More...
 
int s
 Mutex and semaphores are badly broken on Linux, and file locking not accessible from C++ streams. More...
 

Detailed Description

Class to implement reading, writing, adding, deleting, and formatting messages from the SMK .json file.

Constructor & Destructor Documentation

◆ SMK()

SMK::SMK ( void  )

Constructor.

References s.

◆ ~SMK()

SMK::~SMK ( void  )

Destructor.

References s.

Member Function Documentation

◆ add()

SMK & SMK::add ( const std::string &  priority,
const std::string &  msg,
const bool  unique = false 
)

Add or update a message in filename.

If the message already exists, then the count and timestamp are both updated. If unique is set to true, then the count is set to 1 even if the message already existed.

See also
unique()

References clean(), load(), and save().

Referenced by unique().

Here is the caller graph for this function:

◆ clean()

SMK & SMK::clean ( void  )

Clean up old entries in filename. Will only keep the last few recent messages.

References load(), and save().

Referenced by add(), and save().

Here is the caller graph for this function:

◆ del()

SMK & SMK::del ( const std::string &  parm)

Delete a message from the json in filename.

If parm is numeric, then it is assumed to be an index into the message array. If parm is text, then a case-sensitive partial substring match will be done on all of the messages, and those found to match will be deleted.

References load(), and save().

◆ display_msg()

SMK & SMK::display_msg ( const json &  j)
protected

Format the given message or messages as a tab-delimited block of text.

See also
get_all()
get_recent()

Referenced by get(), get_all(), and get_recent().

Here is the caller graph for this function:

◆ dump()

SMK & SMK::dump ( void  )

Dump the full json in filename to stdout.

References load().

◆ format() [1/3]

SMK & SMK::format ( void  )

Format all the message to an easy-to-read block of text. This can be used as a login message.

References load().

◆ format() [2/3]

std::string SMK::format ( const std::time_t  now,
const std::time_t  tt 
)
protected

Format the given timestamp as an easy-to-read text string, such as "35 seconds ago" or "2 days ago".

◆ format() [3/3]

std::string SMK::format ( const int  priority)
protected

Format the numeric priority index to a text string, such as high or low.

◆ get()

SMK & SMK::get ( const std::string &  parm)

Get a specific message from filename. The parm must be a valid numerical index, starting at 1.

References display_msg(), and load().

◆ get_all()

SMK & SMK::get_all ( void  )

Get a tab-delimited set of all messages.

References display_msg(), and load().

◆ get_recent()

SMK & SMK::get_recent ( void  )

Get a tab-delimited set of recent messages.

References display_msg(), and load().

◆ initialize_json()

SMK & SMK::initialize_json ( json &  j)
protected

Insert a few housekeeping items into the json.

Referenced by load(), reset(), and save().

Here is the caller graph for this function:

◆ load()

json SMK::load ( void  )

Load the json from filename.

References filename, and initialize_json().

Referenced by add(), clean(), del(), dump(), format(), get(), get_all(), and get_recent().

Here is the caller graph for this function:

◆ reset()

SMK & SMK::reset ( void  )

Erase the json in filename and start with a brand new json structure.

References initialize_json(), and save().

◆ save()

SMK & SMK::save ( json &  j)

Save the json to filename.

References clean(), filename, initialize_json(), and sort_messages_by_timestamp().

Referenced by add(), clean(), del(), and reset().

Here is the caller graph for this function:

◆ show_usage()

SMK & SMK::show_usage ( const std::string &  arg0)

Show a block of text that describes how to use SMK.

◆ sort_messages_by_timestamp()

SMK & SMK::sort_messages_by_timestamp ( json &  j)
protected

Ensure the messages in filename are sorted in order from oldest to newest.

This is automatically called when the json file is saved to disk.

Referenced by save().

Here is the caller graph for this function:

◆ unique()

SMK & SMK::unique ( const std::string &  priority,
const std::string &  msg 
)

Same thing as add() but sets the unique parameter to true.

See also
add()

References add().

Member Data Documentation

◆ filename

std::string SMK::filename
protected

The location where the json structure is stored. This file must have read/write permissions.

Referenced by load(), and save().

◆ s

int SMK::s
protected

Mutex and semaphores are badly broken on Linux, and file locking not accessible from C++ streams.

Instead, use a socket bound to a specific port to emulate file or process locking.

Referenced by SMK(), and ~SMK().