20 #ifndef TARGET_OS_IPHONE
21 #define TARGET_OS_IPHONE 0
29 #ifdef __ANDROID_API__
30 #include <android/log.h>
39 #ifdef __ANDROID_API__
41 __android_log_print(ANDROID_LOG_VERBOSE,
"BBK",
"%s", s);
44 __android_log_print(ANDROID_LOG_VERBOSE,
"BBK",
"%s", s.c_str());
47 __android_log_print(ANDROID_LOG_VERBOSE,
"BBK",
"%d", i);
50 __android_log_print(ANDROID_LOG_VERBOSE,
"BBK",
"%f", x);
74 typedef std::chrono::steady_clock::time_point
TimePoint;
93 _blackHole.clear(std::istream::eofbit);
112 unsigned int warnlines = 0,
113 unsigned int errlines = 0);
116 static void sayTime(std::ostream &stream);
135 *_logFile <<
"\n" << global_elapsed_ms() <<
' ' <<
label <<
" *** "
136 << (err_count ?
"ERROR ***: " :
"LAST ERR ***: ");
154 *_logFile <<
"\n" << global_elapsed_ms() <<
' ' <<
label <<
" *** "
155 << (warn_count ?
"WARNING ***: " :
"LAST WARN ***: ");
170 static std::ostream &
log(
const std::string &
label) {
173 *_logFile <<
"\n" << global_elapsed_ms() <<
' ' <<
label <<
": ";
175 *_logFile <<
"LAST LOG: ";
188 *_logFile << std::endl;
193 _logFile = &_blackHole;
219 return std::chrono::steady_clock::now();
224 return timeNow() + std::chrono::microseconds(
toUs(s));
229 return TimePoint::max();
233 static std::chrono::microseconds
toUs(
double t) {
234 auto us =
static_cast<std::chrono::microseconds::rep
>(1e6*t);
235 return std::chrono::microseconds(us);
263 #define TASKRUNNER_LOGERR
264 #define TASKRUNNER_LOGWARN
265 #define TASKRUNNER_LOGINFO
266 #define TASKRUNNER_LOGBDG
269 #ifdef TASKRUNNER_LOGERR
302 #ifdef TASKRUNNER_LOGWARN
319 #ifdef TASKRUNNER_LOGINFO
328 std::ostream &
log()
const {
336 #ifdef TASKRUNNER_LOGDBG
346 *_logFile <<
"\n" << global_elapsed_ms() <<
' ' << _label <<
": ";
355 static int64_t global_elapsed_ms() {
356 return msSince(global_start_time);
363 static bool in_error;
371 static std::ostream *_logFile;
375 static std::ostringstream _blackHole;
379 static unsigned int log_count, warn_count, err_count,
380 log_count_saved, warn_count_saved, err_count_saved;
This class is used to optionally disable logging operations at compile time.
Definition: logger.h:34
DummyStream & operator<<(T)
Do nothing, emulating the << stream operator.
Definition: logger.h:38
DummyStream & operator<<(std::ostream &(*)(std::ostream &))
Do nothing, emulating the << stream operator.
Definition: logger.h:54
A simple logger. All classes that want to write to the global log file should inherit from this class...
Definition: logger.h:86
std::string label() const
Return the object's log label.
Definition: logger.h:251
static int64_t msTo(const TimePoint &t)
Definition: logger.cpp:34
static bool inError()
Return true if any error has been logged (globally since start)
Definition: logger.h:119
static void setLogLimit(unsigned int loglines=0, unsigned int warnlines=0, unsigned int errlines=0)
Set max number of lines of info/warn/err log.
Definition: logger.cpp:117
static void flushLogFile()
Definition: logger.h:187
static void reopenLogFile(const std::string &filename)
If current log is a file (ofstream), reopen it with new filename:
Definition: logger.cpp:173
static TimePoint timeMax()
Return a very distant time.
Definition: logger.h:228
Logger(std::string label)
Definition: logger.h:90
static bool hasExpired(const TimePoint &t)
Return true if current time is after the given TimePoint.
Definition: logger.h:213
static int64_t msSince(const TimePoint &t)
Definition: logger.cpp:28
std::ostream & errno_log() const
Write a line of error log after a failed system call has set the global errno to a non-zero value.
Definition: logger.cpp:143
static std::string dateString2(time_t t=0)
Return local time, formatted as Sat, 14 Oct 2023 09:38:47.
Definition: logger.cpp:50
static TimePoint timeNow()
Return current time.
Definition: logger.h:218
static std::string createHashKey(unsigned int length=20)
Return a random string.
Definition: logger.cpp:60
static TimePoint timeAfter(double s)
Return current time plus s seconds.
Definition: logger.h:223
std::ostream & log() const
Write a line of info log.
Definition: logger.h:328
static void pauseLogging()
Disable all log output until next call to Logger::setLogFile.
Definition: logger.h:192
std::ostream & dbg_log() const
Write a line of debug log.
Definition: logger.h:345
static std::string dateString(time_t t=0)
Return local time, formatted as 2023-10-14T09:38:47+0200.
Definition: logger.cpp:40
static double secondsTo(const TimePoint &t)
Definition: logger.cpp:22
static void setLogFile(std::ostream &stream)
Set global log destination.
Definition: logger.cpp:166
void resetLabel(const std::string &new_label)
Modify the object's log label.
Definition: logger.h:256
static std::chrono::microseconds toUs(double t)
Convert s (seconds) to std::chrono::microseconds.
Definition: logger.h:233
std::ostream & warn_log() const
Write a line of warning log.
Definition: logger.h:311
static void sayTime(std::ostream &stream)
Write current local time to the given stream.
Definition: logger.cpp:131
static std::ostream & log(const std::string &label)
Write a line of info log.
Definition: logger.h:170
static std::ostream & warn_log(const std::string &label)
Write a line of warning log.
Definition: logger.h:151
static std::ostream & err_log(const std::string &label)
Write a line of error log.
Definition: logger.h:131
std::ostream & err_log() const
Write a line of error log.
Definition: logger.h:292
static double secondsSince(const TimePoint &t)
Definition: logger.cpp:16
Measure elapsed time during execution, for example by timer events.