#include <sq3_log_db.hpp>
Public Member Functions | |
log_db (std::string const &filename) | |
Opens/creates a db from the given filename. | |
log_db () | |
Creates an unopened database. | |
virtual | ~log_db () |
Closes this db. | |
virtual int | clear () |
Empties the log database. | |
bool | log (std::string const &msg) |
Logs a message to the log database. | |
bool | log (char const *format,...) |
Logs the given printf-formatted string to the database. | |
virtual void | show_last (int howMany) |
Shows the last count entries using a subclass-specific method. | |
bool | trim (int leaveThisMany) |
Deletes all entries in the log except the leaveThisMany most recent. | |
Protected Member Functions | |
virtual int | on_open () |
Called when open() succeeds. |
It is intended to be a small logger for simple systems, and not a powerful logging system. The main advantages of using an sqlite3 db instead of a conventional log file are:
In the case that you want to query the data, this type creates a table named 'log' with the fields (ts,msg), containing the timestamp and log message, respectively.
Definition at line 28 of file sq3_log_db.hpp.
sq3::log_db::log_db | ( | std::string const & | filename | ) | [explicit] |
Opens/creates a db from the given filename.
Note that this function cannot notify the user if the file cannot be created or if the file is not really a DB file (in which case all logging will silently fail)! Use this->is_open() to find out if the open() worked!
Note that it is not legal to have more than one instance with the same filename - the second instance will not be able to open the database! It is also not strictly legal to re-use an existing non-log_db database.
Definition at line 10 of file sq3_log_db.cpp.
References sq3::database::open().
int sq3::log_db::clear | ( | ) | [virtual] |
Empties the log database.
Returns SQLITE_OK on success or some other value on error (e.g., db is not open).
Reimplemented from sq3::database.
Definition at line 36 of file sq3_log_db.cpp.
References sq3::database::execute().
bool sq3::log_db::log | ( | char const * | format, | |
... | ||||
) |
Logs the given printf-formatted string to the database.
If the resulting string is "too long" then it is internally truncated. "Too long" is rather arbitrarily chosen to be 2k of text.
If the format string evaluates to an empty string, this function returns true but does not log the entry.
In gcc, vsnprintf() is in the std namespace, but in MSVC it is not, so we use 'using' to accomodate both cases.
Definition at line 58 of file sq3_log_db.cpp.
References sq3::statement::bind(), sq3::statement::execute(), and sq3::database::is_open().
bool sq3::log_db::log | ( | std::string const & | msg | ) |
Logs a message to the log database.
Returns true on success, false on error.
If the format string evaluates to an empty string, this function returns true but does not log the entry.
Definition at line 44 of file sq3_log_db.cpp.
References sq3::statement::bind(), sq3::statement::execute(), sq3::database::is_open(), and sq3::rc_is_okay().
int sq3::log_db::on_open | ( | ) | [protected, virtual] |
Called when open() succeeds.
Reimplemented to create the logging table if it's not already in the db.
enable temp_store=MEMORY to speed this up considably on PocketPC devices writing to SD cards.
Reimplemented from sq3::database.
Definition at line 20 of file sq3_log_db.cpp.
References sq3::database::execute(), sq3::database::is_open(), and sq3::database::pragma().
void sq3::log_db::show_last | ( | int | howMany | ) | [virtual] |
Shows the last count entries using a subclass-specific method.
The default is to send the items to std::cout. A subclass could override this to show a dialog box, for example. A subclass which does so may also want to call the base implementation but certainly doesn't need to.
Subclasses are requested to respect the howMany argument, but may of course format the data to suit their desires.
Definition at line 96 of file sq3_log_db.cpp.
References sq3::cursor::get(), sq3::database::is_open(), and sq3::cursor::step().
bool sq3::log_db::trim | ( | int | leaveThisMany | ) |
Deletes all entries in the log except the leaveThisMany most recent.
e.g. Trim( 5 ) leaves only the most recent 5 entries in the database. The intent of this function is to give applications a way of keeping the log size small without having to use Clear() to empty the whole database.
Returns true on success, false on error.
Definition at line 139 of file sq3_log_db.cpp.
References sq3::database::execute(), sq3::database::is_open(), and sq3::database::vacuum().