3#ifndef _FRAY_LOG_MANAGER_HPP_
4#define _FRAY_LOG_MANAGER_HPP_
150 std::chrono::system_clock::time_point now = std::chrono::system_clock::now())
152 using namespace std::chrono;
153 auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
155 std::time_t t = system_clock::to_time_t(now);
158 localtime_s(&tm, &t);
160 localtime_r(&t, &tm);
164 timestamp, 32,
"[%02d:%02d:%02d:%03d] [%02d.%02d.%02d]",
165 tm.tm_hour, tm.tm_min, tm.tm_sec,
166 static_cast<F_long>(ms.count()),
167 tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday);
177 using namespace std::chrono;
178 auto now = system_clock::now();
179 std::time_t t = system_clock::to_time_t(now);
182 localtime_s(&tm, &t);
184 localtime_r(&t, &tm);
188 date, 32,
"%02d.%02d.%02d",
189 tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday);
199 std::chrono::system_clock::time_point now = std::chrono::system_clock::now())
201 using namespace std::chrono;
202 auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
204 std::time_t t = system_clock::to_time_t(now);
207 localtime_s(&tm, &t);
209 localtime_r(&t, &tm);
213 time, 32,
"%02d:%02d:%02d.%03d",
214 tm.tm_hour, tm.tm_min, tm.tm_sec,
215 static_cast<int>(ms.count()));
224 static std::atomic<F_long> next{0};
225 thread_local const F_long id = next++;
248 static std::mutex mutex;
257 static std::shared_ptr<Sink> slot;
271 std::lock_guard<std::mutex> lock(
sinkMutex());
272 sinkSlot() = sink ? std::make_shared<Sink>(std::move(sink)) :
nullptr;
283 thread_local std::string project;
307 const std::string &project)
311 if (!project.empty())
336 thread_local bool suppressed =
false;
370 thread_local bool visible =
true;
408 std::string line =
"[";
424 static std::mutex outputMutex;
425 std::unique_lock<std::mutex> lock(outputMutex);
426 std::fwrite(line.data(), 1, line.size(), stdout);
430 std::shared_ptr<Sink> sink;
432 std::lock_guard<std::mutex> sinkLock(
sinkMutex());
542 const std::string& name)
611 F_err _err = (func); \
612 if (_err != fray::errors::NONE) \
614 fray::log::engineError(fray::errors::description(_err)); \
626 F_err _err = (func); \
627 if (_err != fray::errors::NONE) \
629 fray::log::engineWarn("%s", fray::errors::description(_err)); \
~ConsoleSuppression()
Restores the previous Console visibility.
ConsoleSuppression(const ConsoleSuppression &)=delete
ConsoleSuppression & operator=(const ConsoleSuppression &)=delete
ConsoleSuppression()
Suppresses Console delivery on the calling thread.
ProjectScope(const std::string &project)
Save the calling thread's tag and optionally replace it.
std::string _previous
Project tag to restore when this scope ends.
~ProjectScope()
Restore the saved tag on the calling thread.
ProjectScope & operator=(const ProjectScope &)=delete
Disallow copy assignment of a project tag scope.
ProjectScope(const ProjectScope &)=delete
Disallow copying a project tag scope.
SinkSuppression & operator=(const SinkSuppression &)=delete
Disallow copy assignment of a suppression scope.
bool _previous
Setting to restore when this scope ends.
SinkSuppression()
Suppresses the sink on the calling thread.
SinkSuppression(const SinkSuppression &)=delete
Disallow copying a suppression scope.
~SinkSuppression()
Restores the previous setting on the calling thread.
bool & consoleVisible()
Whether the calling thread's log entries may be shown in the Console.
std::string & threadProject()
Return the calling thread's project tag; an empty tag denotes the application.
F_void engineInfo(const F_char *fmt,...)
Log info for Engine.
constexpr F_long DATE_SIZE
Date buffer size.
F_void engineError(const F_char *fmt,...)
Log error for Engine.
F_void engineWarn(const F_char *fmt,...)
Log warning for Engine.
constexpr const F_char * ANSI_FG_WHITE
ANSI FG white.
F_long threadId()
Small sequential id for the calling thread, assigned on its first call (the first caller is 0).
constexpr const F_char * ANSI_BG_YELLOW
ANSI BG yellow.
constexpr const F_char * ANSI_INVERSE
ANSI inverse.
constexpr const F_char * ANSI_FG_MAGENTA
ANSI FG magenta.
std::mutex & sinkMutex()
Return the mutex protecting access to the shared sink slot.
constexpr const F_char * ANSI_FG_ORANGE
ANSI FG orange (256-color).
F_void setSink(Sink sink)
Replace the sink; an empty function removes it. Locks sinkMutex; an in-flight logging call may still ...
F_void electronError(const F_char *fmt,...)
Log error for Electron.
F_char[TIME_SIZE] timeBuffer
Time buffer type.
bool & sinkSuppressed()
Whether the calling thread's printed lines skip the sink, so they reach stdout only.
constexpr F_long TIME_SIZE
Time buffer size.
constexpr const F_char * ANSI_BG_BLACK
ANSI BG black.
F_void getDate(F_char *date)
Write "YY.MM.DD" into buffer.
constexpr const F_char * ANSI_BG_MAGENTA
ANSI BG magenta.
constexpr const F_char * ANSI_BG_GREEN
ANSI BG green.
F_void engineFatal(const F_char *fmt,...)
Log fatal for Engine.
constexpr const F_char * ANSI_BG_WHITE
ANSI BG white.
F_void electronWarn(const F_char *fmt,...)
Log warning for Electron.
constexpr const F_char * ANSI_BG_CYAN
ANSI BG cyan.
constexpr const F_char * ANSI_BG_BLUE
ANSI BG blue.
constexpr const F_char * ANSI_FG_BLUE
ANSI FG blue.
constexpr const F_char * ANSI_BG_RED
ANSI BG red.
F_void engineSession(const std::string &name)
Marks the start of a log session under the calling thread's project tag. The Engine records it as a s...
constexpr const F_char * ANSI_BG_ORANGE
ANSI BG orange (256-color).
constexpr const F_char * ANSI_FG_CYAN
ANSI FG cyan.
constexpr const F_char * ANSI_RESET
ANSI reset code.
F_char[DATE_SIZE] dateBuffer
Date buffer type.
F_void printf(const F_char *PID, const F_char *PIDColor, const F_char *level, const F_char *levelColor, const F_char *message)
Print a formatted log line with PID, level and colors. Serializes and flushes stdout,...
constexpr const F_char * ANSI_FG_GREEN
ANSI FG green.
constexpr const F_char * ANSI_UNDERLINE
ANSI underline.
F_void vprintf_helper(const F_char *PID, const F_char *PIDColor, const F_char *level, const F_char *levelColor, const F_char *fmt, va_list args)
Format up to 1023 message bytes and delegate to printf().
F_void generateTimestamp(F_char *timestamp, std::chrono::system_clock::time_point now=std::chrono::system_clock::now())
Write "[HH:MM:SS:mmm] [YY.MM.DD]" into buffer.
std::shared_ptr< Sink > & sinkSlot()
Return the shared sink slot; callers must hold sinkMutex when accessing it.
constexpr const F_char * ANSI_BLINK
ANSI blink.
F_void electronInfo(const F_char *fmt,...)
Log info for Electron.
constexpr const F_char * ANSI_FG_BLACK
ANSI FG black.
constexpr const F_char * ANSI_FG_YELLOW
ANSI FG yellow.
F_void getTime(F_char *time, std::chrono::system_clock::time_point now=std::chrono::system_clock::now())
Write "HH:MM:SS.mmm" into buffer.
F_void electronFatal(const F_char *fmt,...)
Log fatal for Electron.
constexpr F_long TIMESTAMP_SIZE
Timestamp buffer size.
std::function< void(const F_char *PID, const F_char *level, F_long thread, const std::string &project, const F_char *message, bool consoleVisible)> Sink
Receives each printed line synchronously on the logging thread; it must not log. The sink runs withou...
constexpr const F_char * ANSI_FG_RED
ANSI FG red.
F_char[TIMESTAMP_SIZE] TimestampBuffer
Timestamp buffer type.
constexpr const F_char * ANSI_BOLD
ANSI bold.
int32_t F_long
Signed 32-bit integer (also the base of F_err).
void F_void
Alias for void.
char F_char
Alias for char.