EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
echomap::Logger Class Reference

A portable (native and browser) and thread-safe logger implementation for formatted messages. More...

#include <Logger.hpp>

Public Types

enum class  Level { Debug , Info , Warning , Error }
 Indicates the level of importance of a logged message. More...

Static Public Member Functions

static void log (Level level, std::string_view message, std::source_location location=std::source_location::current())
 Log an unformatted message to the backend.
template<typename... Args>
static void log_f (const Level level, const std::source_location location, std::format_string< Args... > fmt, Args &&... args)
 Log a formatted message to the backend with a specific source location.

Static Private Member Functions

static std::string_view level_to_string (Level level)
 Get a human-readable string corresponding to the given log level.

Detailed Description

A portable (native and browser) and thread-safe logger implementation for formatted messages.

If compiled for a native backend, the standard C++20 synchronised IO stream is used. If compiled for WebAssembly, the JavaScript API is used to produce log messages in the browser console.

Definition at line 24 of file Logger.hpp.

Member Enumeration Documentation

◆ Level

enum class echomap::Logger::Level
strong

Indicates the level of importance of a logged message.

Definition at line 30 of file Logger.hpp.

31 {
32 Debug,
33 Info,
34 Warning,
35 Error
36 };

Member Function Documentation

◆ level_to_string()

std::string_view echomap::Logger::level_to_string ( Level level)
staticprivate

Get a human-readable string corresponding to the given log level.

Parameters
levelRelevant log level.
Returns
Human-readable string for the log level.

Definition at line 59 of file Logger.cpp.

62{
63 switch (level) {
64 case Level::Debug:
65 return "Debug";
66 case Level::Error:
67 return "Error";
68 case Level::Info:
69 return "Info";
70 case Level::Warning:
71 return "Warning";
72 }
73
74 return "Unknown";
75}

◆ log()

void echomap::Logger::log ( Level level,
std::string_view message,
std::source_location location = std::source_location::current() )
static

Log an unformatted message to the backend.

Parameters
levelThe level of the message.
messageThe unformatted message (string literal).
locationThe optional origin of the message.

Definition at line 22 of file Logger.cpp.

27{
28 const auto line = std::format(
29 "[{} ({})] {}:{} says: {}",
30 level_to_string(level),
32 location.file_name(),
33 location.line(),
34 message
35 );
36
37#if defined(__EMSCRIPTEN__) || defined(__DOXYGEN__)
38 switch (level) {
39 case Level::Error:
40 emscripten_console_error(line.c_str());
41 break;
42 case Level::Info:
43 emscripten_console_log(line.c_str());
44 break;
45 case Level::Warning:
46 emscripten_console_warn(line.c_str());
47 break;
48 case Level::Debug:
49 // Do not use console.trace, since Chrome DevTools expands it.
50 emscripten_console_log(line.c_str());
51 break;
52 }
53#else
54 auto stream = std::osyncstream(level == Level::Error ? std::cerr : std::cout);
55 stream << line << '\n';
56#endif
57}
static std::string_view level_to_string(Level level)
Get a human-readable string corresponding to the given log level.
Definition Logger.cpp:59
T file_name(T... args)
T format(T... args)
T get_id(T... args)

◆ log_f()

template<typename... Args>
void echomap::Logger::log_f ( const Level level,
const std::source_location location,
std::format_string< Args... > fmt,
Args &&... args )
inlinestatic

Log a formatted message to the backend with a specific source location.

Template Parameters
ArgsTypes of variadic arguments for the formatter.
Parameters
levelThe level of the message.
locationThe origin of the message.
fmtThe printf-style format string of the message.
argsThe values for the format string.

Definition at line 61 of file Logger.hpp.

67 {
68 log(level, std::format(fmt, std::forward<Args>(args)...), location);
69 }
static void log(Level level, std::string_view message, std::source_location location=std::source_location::current())
Log an unformatted message to the backend.
Definition Logger.cpp:22
T forward(T... args)

The documentation for this class was generated from the following files: