#include "logger.h" /// Logger that writes to a file class FileLogDriver : public LogDriverBase { public: FileLogDriver(int level, const std::string & path) : LogDriverBase(level), file(fopen(path.c_str(), "a")) { } ~FileLogDriver() { fclose(file); } void message(int priority, const char * msg) const { if (priority <= level) { fprintf(file, "%s %-6.*s%s\n", timeStr(), 5, Log::priorityName(level), msg); fflush(file); } } private: FILE * file; const char * timeStr() const { struct tm tm; time_t t = time(NULL); localtime_r(&t, &tm); strftime(tmbuf, sizeof(tmbuf), "%F %T", &tm); return tmbuf; } mutable char tmbuf[30]; }; DECLARE_GENERIC_LOADER("file", LogDriverLoader, FileLogDriver);