35 struct DestructionDetector {
36 bool destructed =
false;
38 ~DestructionDetector() { destructed =
true; }
39 } destructionDetector;
44 Log::Level outputLevel = Log::Level::INFORMATIONAL;
45 bool extendedFormat =
false;
46 unique_ptr<string> hostnameStr;
48 atomic_uint instanceCount(0);
56 explicit Data(Level defaultLevel = Level::INFORMATIONAL) : defaultLevel(defaultLevel) {}
62 if (instanceCount == 0) {
73 data->appname =
"nawa";
79 data->appname = std::move(appname);
80 data->defaultLevel = level;
84 data->defaultLevel = level;
88 data = make_unique<Data>(*other.data);
101 if (!destructionDetector.destructed) {
103 if (instanceCount == 0) {
104 if (logFile.is_open()) {
110 if (logFile.is_open()) {
124 if (logFile.is_open()) {
127 logFile.open(filename, ofstream::out | ofstream::app);
130 "Failed to open requested file for writing.");
144 extendedFormat = useExtendedFormat;
157 data->appname = std::move(appname);
161 data->defaultLevel = level;
165 write(msg, data->defaultLevel);
170 auto now = time(
nullptr);
171 lock_guard<mutex> l(outLock);
172 if (extendedFormat) {
173 *out << put_time(localtime(&now),
"%b %d %H:%M:%S ") << *hostnameStr <<
' '
176 cerr <<
"[" << data->appname <<
"] " << msg << endl;
182 write(msg, data->defaultLevel);
Exception class that can be used by apps to catch errors resulting from nawa function calls.
Simple class for (not (yet) thread-safe) logging to stderr or to any other output stream.
Log & operator=(Log const &other) noexcept
static void setOutputLevel(Level level)
void operator()(std::string const &msg)
static void setOutfile(std::string const &filename)
void setDefaultLogLevel(Level level) noexcept
static void lockStream() noexcept
void write(std::string const &msg)
void setAppname(std::string appname) noexcept
static bool isLocked() noexcept
static void setStream(std::ostream *os) noexcept
static void setExtendedFormat(bool useExtendedFormat)
std::string getProgramInvocationName()
std::string getSystemHostname()
This file contains helpers for operating-system specific stuff.