MUSX Document Model
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Logger.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include <functional>
25#include <iostream>
26#include <string>
27
28namespace musx {
29namespace util {
30
39class Logger {
40public:
44 enum class LogLevel {
45 Info,
46 Warning,
47 Error,
48 Verbose
49 };
50
56 using LogCallback = std::function<void(Logger::LogLevel, const std::string&)>;
57
65 static void setCallback(LogCallback callback) {
66 getInstance().m_callback = callback;
67 }
68
75 return getInstance().m_callback;
76 }
77
87 static void log(LogLevel level, const std::string& message) {
88 if (getInstance().m_callback) {
89 getInstance().m_callback(level, message);
90 } else {
91 std::cerr << message << std::endl;
92 }
93 }
94
95private:
99 Logger() = default;
100
106 static Logger& getInstance() {
107 static Logger instance;
108 return instance;
109 }
110
112 LogCallback m_callback;
113};
114
115} // namespace util
116} // namespace musx
A utility class for handling logging with customizable callbacks.
Definition Logger.h:39
LogLevel
Enum class representing the severity levels of log messages.
Definition Logger.h:44
@ Warning
Warning messages indicating potential issues.
@ Info
Informational messages for general program operation.
@ Error
Error messages indicating critical issues.
@ Verbose
Informational messages that should only displayed when verbose logging is requested.
static void log(LogLevel level, const std::string &message)
Logs a message with a specific severity level.
Definition Logger.h:87
static LogCallback getCallback()
Retrieves the currently set logging callback.
Definition Logger.h:74
static void setCallback(LogCallback callback)
Sets a custom callback for handling log messages.
Definition Logger.h:65
std::function< void(Logger::LogLevel, const std::string &)> LogCallback
Type alias for the logging callback function.
Definition Logger.h:56
object model for musx file (enigmaxml)
Definition BaseClasses.h:32