sq3::log_db Class Reference

log_db is a simple logging database for use with arbitrary applications. More...

#include <sq3_log_db.hpp>

Inheritance diagram for sq3::log_db:
sq3::database

List of all members.

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.

Detailed Description

log_db is a simple logging database for use with arbitrary applications.

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.


Constructor & Destructor Documentation

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 5 of file sq3_log_db.cpp.

References log_db().

sq3::log_db::log_db (  ) 

Creates an unopened database.

Use this->open() to open it.

Referenced by log_db().


Member Function Documentation

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 31 of file sq3_log_db.cpp.

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 53 of file sq3_log_db.cpp.

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 39 of file sq3_log_db.cpp.

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 15 of file sq3_log_db.cpp.

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 91 of file sq3_log_db.cpp.

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 134 of file sq3_log_db.cpp.


The documentation for this class was generated from the following files:
Generated by  doxygen 1.6.2-20100208