#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) { struct tm tm; time_t t = time(NULL); localtime_r(&t, &tm); char tmbuf[30]; strftime(tmbuf, sizeof(tmbuf), "%F %T", &tm); fprintf(file, "%s %-6.*s%s\n", tmbuf, 5, Log::priorityName(level), msg); fflush(file); } } private: FILE * file; }; DECLARE_GENERIC_LOADER("file", LogDriverLoader, FileLogDriver);