Fray Engine
Loading...
Searching...
No Matches
log_manager.hpp File Reference
#include <cstdio>
#include <ctime>
#include <chrono>
#include <string>
#include <atomic>
#include <cstdarg>
#include <functional>
#include <memory>
#include <mutex>
#include "types.hpp"
#include "errors.hpp"

Go to the source code of this file.

Classes

class  fray::log::ProjectScope
 Temporarily tag log lines on the calling thread, then restore its previous tag. Construct and destroy the scope on the same thread. More...
class  fray::log::SinkSuppression
 Keeps the calling thread's printed lines out of the sink while it lives, then restores the previous setting. Used for lines that are recorded elsewhere under their own identity. Construct and destroy on the same thread. More...
class  fray::log::ConsoleSuppression
 Keeps lines in the persistent log but out of the Console for this thread's scope. More...

Namespaces

namespace  fray
namespace  fray::log

Macros

#define ERR(func)
 Evaluate function and return on error.
#define ERR2(func)
 Evaluate function and log errors without returning.

Typedefs

using fray::log::TimestampBuffer = F_char[TIMESTAMP_SIZE]
 Timestamp buffer type.
using fray::log::dateBuffer = F_char[DATE_SIZE]
 Date buffer type.
using fray::log::timeBuffer = F_char[TIME_SIZE]
 Time buffer type.
using fray::log::Sink = std::function<void(const F_char *PID, const F_char *level, F_long thread, const std::string &project, const F_char *message, bool consoleVisible)>
 Receives each printed line synchronously on the logging thread; it must not log. The sink runs without the output or sink mutex held; concurrent calls may overlap.

Functions

F_void fray::log::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.
F_void fray::log::getDate (F_char *date)
 Write "YY.MM.DD" into buffer.
F_void fray::log::getTime (F_char *time, std::chrono::system_clock::time_point now=std::chrono::system_clock::now())
 Write "HH:MM:SS.mmm" into buffer.
F_long fray::log::threadId ()
 Small sequential id for the calling thread, assigned on its first call (the first caller is 0).
std::mutex & fray::log::sinkMutex ()
 Return the mutex protecting access to the shared sink slot.
std::shared_ptr< Sink > & fray::log::sinkSlot ()
 Return the shared sink slot; callers must hold sinkMutex when accessing it.
F_void fray::log::setSink (Sink sink)
 Replace the sink; an empty function removes it. Locks sinkMutex; an in-flight logging call may still invoke the previous sink.
std::string & fray::log::threadProject ()
 Return the calling thread's project tag; an empty tag denotes the application.
bool & fray::log::sinkSuppressed ()
 Whether the calling thread's printed lines skip the sink, so they reach stdout only.
bool & fray::log::consoleVisible ()
 Whether the calling thread's log entries may be shown in the Console.
F_void fray::log::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, then invokes a snapshot of the sink on the calling thread without holding either mutex. The sink must not log and may run concurrently.
F_void fray::log::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 fray::log::electronInfo (const F_char *fmt,...)
 Log info for Electron.
F_void fray::log::electronWarn (const F_char *fmt,...)
 Log warning for Electron.
F_void fray::log::electronError (const F_char *fmt,...)
 Log error for Electron.
F_void fray::log::electronFatal (const F_char *fmt,...)
 Log fatal for Electron.
F_void fray::log::engineInfo (const F_char *fmt,...)
 Log info for Engine.
F_void fray::log::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 session row, which the Console shows as a divider before the session's entries.
F_void fray::log::engineInfo (const std::string &message)
 Print an Engine info line without interpreting the message as a format string.
F_void fray::log::engineWarn (const F_char *fmt,...)
 Log warning for Engine.
F_void fray::log::engineError (const F_char *fmt,...)
 Log error for Engine.
F_void fray::log::engineFatal (const F_char *fmt,...)
 Log fatal for Engine.

Variables

constexpr const F_char * fray::log::ANSI_RESET = "\033[0m"
 ANSI reset code.
constexpr const F_char * fray::log::ANSI_BOLD = "\033[1m"
 ANSI bold.
constexpr const F_char * fray::log::ANSI_UNDERLINE = "\033[4m"
 ANSI underline.
constexpr const F_char * fray::log::ANSI_BLINK = "\033[5m"
 ANSI blink.
constexpr const F_char * fray::log::ANSI_INVERSE = "\033[7m"
 ANSI inverse.
constexpr const F_char * fray::log::ANSI_FG_BLACK = "\033[30m"
 ANSI FG black.
constexpr const F_char * fray::log::ANSI_FG_RED = "\033[31m"
 ANSI FG red.
constexpr const F_char * fray::log::ANSI_FG_GREEN = "\033[32m"
 ANSI FG green.
constexpr const F_char * fray::log::ANSI_FG_YELLOW = "\033[33m"
 ANSI FG yellow.
constexpr const F_char * fray::log::ANSI_FG_BLUE = "\033[34m"
 ANSI FG blue.
constexpr const F_char * fray::log::ANSI_FG_MAGENTA = "\033[35m"
 ANSI FG magenta.
constexpr const F_char * fray::log::ANSI_FG_CYAN = "\033[36m"
 ANSI FG cyan.
constexpr const F_char * fray::log::ANSI_FG_WHITE = "\033[37m"
 ANSI FG white.
constexpr const F_char * fray::log::ANSI_FG_ORANGE = "\x1b[38;5;208m"
 ANSI FG orange (256-color).
constexpr const F_char * fray::log::ANSI_BG_BLACK = "\033[40m"
 ANSI BG black.
constexpr const F_char * fray::log::ANSI_BG_RED = "\033[41m"
 ANSI BG red.
constexpr const F_char * fray::log::ANSI_BG_GREEN = "\033[42m"
 ANSI BG green.
constexpr const F_char * fray::log::ANSI_BG_YELLOW = "\033[43m"
 ANSI BG yellow.
constexpr const F_char * fray::log::ANSI_BG_BLUE = "\033[44m"
 ANSI BG blue.
constexpr const F_char * fray::log::ANSI_BG_MAGENTA = "\033[45m"
 ANSI BG magenta.
constexpr const F_char * fray::log::ANSI_BG_CYAN = "\033[46m"
 ANSI BG cyan.
constexpr const F_char * fray::log::ANSI_BG_WHITE = "\033[47m"
 ANSI BG white.
constexpr const F_char * fray::log::ANSI_BG_ORANGE = "\033[48;5;208m"
 ANSI BG orange (256-color).
constexpr F_long fray::log::TIMESTAMP_SIZE = 32
 Timestamp buffer size.
constexpr F_long fray::log::DATE_SIZE = 32
 Date buffer size.
constexpr F_long fray::log::TIME_SIZE = 32
 Time buffer size.

Macro Definition Documentation

◆ ERR

#define ERR ( func)
Value:
do \
{ \
F_err _err = (func); \
if (_err != fray::errors::NONE) \
{ \
fray::log::engineError(fray::errors::description(_err)); \
return _err; \
} \
} while (0)
F_long F_err
Error return type used across the Engine; the value is a fray::errors::code (0 / NONE means success).
Definition errors.hpp:13
const F_char * description(code err)
Formats an error code as "Error <number>: <description>". Not thread-safe: the text lives in a shared...
Definition errors.hpp:96

Evaluate function and return on error.

Parameters
funcExpression yielding F_err.

Definition at line 608 of file log_manager.hpp.

◆ ERR2

#define ERR2 ( func)
Value:
do \
{ \
F_err _err = (func); \
if (_err != fray::errors::NONE) \
{ \
fray::log::engineWarn("%s", fray::errors::description(_err)); \
} \
} while (0)

Evaluate function and log errors without returning.

Parameters
funcExpression yielding F_err.

Definition at line 623 of file log_manager.hpp.