#include <file_logger.h>


Public Types | |
| enum | limits { DEFAULT_LOG_FILE_SIZE = 0x10F00D } |
Public Member Functions | |
| file_logger () | |
| creates a logger without a log file and with the default size limit. | |
| file_logger (const basis::astring &filename, int limit=DEFAULT_LOG_FILE_SIZE) | |
| constructs a logger using the "filename" for output. | |
| virtual | ~file_logger () |
| DEFINE_CLASS_NAME ("file_logger") | |
| bool | good () const |
| returns true if the logger appears correctly hooked up to a file. | |
| bool | reopen () |
| closes the current file and attempts to reopen it. | |
| basis::outcome | log (const basis::base_string &info, int filter=basis::ALWAYS_PRINT) |
| writes information to the log file (if the filename is valid). | |
| basis::outcome | log_bytes (const basis::byte_array &to_log, int filter=basis::ALWAYS_PRINT) |
| sends a stream of bytes "to_log" without interpretation into the log. | |
| basis::outcome | format_bytes (const basis::byte_array &to_log, int filter=basis::ALWAYS_PRINT) |
| fancifully formats a stream of bytes "to_log" and sends them into log. | |
| basis::astring | name () const |
| observes the filename where logged information is written. | |
| void | name (const basis::astring &new_name) |
| modifies the filename where logged information will be written. | |
| int | limit () const |
| observes the allowable size of the log file. | |
| void | limit (int new_limit) |
| modifies the allowable size of the log file. | |
| void | flush () |
| causes any pending writes to be sent to the output file. | |
| void | truncate (size_t new_size) |
| chops the file to ensure it doesn't go much over the file size limit. | |
Static Public Member Functions | |
| static basis::astring | log_file_for_app_name () |
| returns a log file name for file_logger based on the program name. | |
Definition at line 37 of file file_logger.h.
Definition at line 53 of file file_logger.h.
| loggers::file_logger::file_logger | ( | ) |
creates a logger without a log file and with the default size limit.
the log file name can be changed using filename().
Definition at line 61 of file file_logger.cpp.
References name().
| loggers::file_logger::file_logger | ( | const basis::astring & | filename, | |
| int | limit = DEFAULT_LOG_FILE_SIZE | |||
| ) |
constructs a logger using the "filename" for output.
there will be no logging if the "filename" is empty. the "limit" specifies how large the log file can be (in bytes).
Definition at line 70 of file file_logger.cpp.
References name().
| loggers::file_logger::~file_logger | ( | ) | [virtual] |
Definition at line 81 of file file_logger.cpp.
References basis::WHACK().
| loggers::file_logger::DEFINE_CLASS_NAME | ( | "file_logger" | ) |
| void loggers::file_logger::flush | ( | ) |
causes any pending writes to be sent to the output file.
Definition at line 136 of file file_logger.cpp.
References filesystem::byte_filer::flush().
Referenced by log(), and log_bytes().
| outcome loggers::file_logger::format_bytes | ( | const basis::byte_array & | to_log, | |
| int | filter = basis::ALWAYS_PRINT | |||
| ) |
fancifully formats a stream of bytes "to_log" and sends them into log.
Definition at line 235 of file file_logger.cpp.
References filesystem::byte_filer::good(), basis::array< contents >::length(), log(), and loggers::filter_set::member().
| bool loggers::file_logger::good | ( | ) | const |
returns true if the logger appears correctly hooked up to a file.
note that we don't open the file when file_logger is constructed; it is only opened once the first logging is attempted.
Definition at line 122 of file file_logger.cpp.
References filesystem::byte_filer::good().
| void loggers::file_logger::limit | ( | int | new_limit | ) | [inline] |
modifies the allowable size of the log file.
Definition at line 89 of file file_logger.h.
| int loggers::file_logger::limit | ( | ) | const [inline] |
observes the allowable size of the log file.
Definition at line 87 of file file_logger.h.
| outcome loggers::file_logger::log | ( | const basis::base_string & | info, | |
| int | filter = basis::ALWAYS_PRINT | |||
| ) | [virtual] |
writes information to the log file (if the filename is valid).
the "filter" value is checked to see if it is in the current set of allowed filters. a value of zero is always printed. if the filename() has not been set, then the information is lost.
Implements basis::base_logger.
Reimplemented in loggers::combo_logger.
Definition at line 178 of file file_logger.cpp.
References flush(), filesystem::byte_filer::good(), basis::astring::length(), basis::base_string::length(), loggers::filter_set::member(), basis::base_string::observe(), basis::astring::s(), filesystem::byte_filer::tell(), truncate(), and filesystem::byte_filer::write().
Referenced by format_bytes().
| outcome loggers::file_logger::log_bytes | ( | const basis::byte_array & | to_log, | |
| int | filter = basis::ALWAYS_PRINT | |||
| ) |
sends a stream of bytes "to_log" without interpretation into the log.
if the "filter" is not enabled, then the info is just tossed out.
Definition at line 209 of file file_logger.cpp.
References flush(), filesystem::byte_filer::good(), basis::array< contents >::length(), loggers::filter_set::member(), basis::array< contents >::observe(), filesystem::byte_filer::tell(), truncate(), and filesystem::byte_filer::write().
| basis::astring loggers::file_logger::log_file_for_app_name | ( | ) | [static] |
returns a log file name for file_logger based on the program name.
for a program named myapp.exe, this will be in the form: {logging_dir}/myapp.log
Definition at line 88 of file file_logger.cpp.
References filesystem::filename::rootname().
| void loggers::file_logger::name | ( | const basis::astring & | new_name | ) |
modifies the filename where logged information will be written.
if "new_name" is blank, then the logged information will not be saved.
Definition at line 109 of file file_logger.cpp.
| astring loggers::file_logger::name | ( | ) | const |
observes the filename where logged information is written.
Definition at line 130 of file file_logger.cpp.
Referenced by file_logger(), reopen(), and truncate().
| bool loggers::file_logger::reopen | ( | ) |
closes the current file and attempts to reopen it.
this is handy if the original opening of the file failed.
Definition at line 94 of file file_logger.cpp.
References name().
| void loggers::file_logger::truncate | ( | size_t | new_size | ) |
chops the file to ensure it doesn't go much over the file size limit.
this can be used externally also, but be careful with it.
// our synchronization scheme allows us to use this inter-application lock; the logger's own lock is always acquired first. no one else can grab the "file_lock", so no deadlocks.
rendezvous file_lock(*_filename + "_trunclock"); if (!file_lock.healthy()) { critical_events::write_to_critical_events((astring("could not create " "lock for ") + *_filename).s()); return; } waiting forever until the file lock succeeds. as long as there are no deadlocks permitted, then this shouldn't be too dangerous... bool got_lock = file_lock.lock(rendezvous::ENDLESS_WAIT); if (!got_lock) { critical_events::write_to_critical_events((astring("could not acquire " "lock for ") + *_filename).s()); return; }
file_lock.unlock();
file_lock.unlock(); // repeal the process-wide lock.
Definition at line 266 of file file_logger.cpp.
References filesystem::byte_filer::eof(), filesystem::byte_filer::good(), loggers::MAXIMUM_BUFFER_SIZE, name(), filesystem::byte_filer::read(), basis::astring::s(), filesystem::byte_filer::seek(), loggers::static_chaos(), filesystem::byte_filer::tell(), unlink, basis::WHACK(), and filesystem::byte_filer::write().
Referenced by log(), and log_bytes().
1.6.3