Add thread safe log prefix assignment
This commit is contained in:
parent
56fb1b9cba
commit
d63c68af78
@ -21,6 +21,7 @@
|
|||||||
#include "config/user_config.hpp"
|
#include "config/user_config.hpp"
|
||||||
#include "network/network_config.hpp"
|
#include "network/network_config.hpp"
|
||||||
#include "utils/file_utils.hpp"
|
#include "utils/file_utils.hpp"
|
||||||
|
#include "utils/tls.hpp"
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
@ -42,10 +43,21 @@
|
|||||||
Log::LogLevel Log::m_min_log_level = Log::LL_VERBOSE;
|
Log::LogLevel Log::m_min_log_level = Log::LL_VERBOSE;
|
||||||
bool Log::m_no_colors = false;
|
bool Log::m_no_colors = false;
|
||||||
FILE* Log::m_file_stdout = NULL;
|
FILE* Log::m_file_stdout = NULL;
|
||||||
std::string Log::m_prefix = "";
|
|
||||||
size_t Log::m_buffer_size = 1;
|
size_t Log::m_buffer_size = 1;
|
||||||
bool Log::m_console_log = true;
|
bool Log::m_console_log = true;
|
||||||
Synchronised<std::vector<struct Log::LineInfo> > Log::m_line_buffer;
|
Synchronised<std::vector<struct Log::LineInfo> > Log::m_line_buffer;
|
||||||
|
thread_local char g_prefix[11] = {};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
void Log::setPrefix(const char* prefix)
|
||||||
|
{
|
||||||
|
size_t len = strlen(prefix);
|
||||||
|
if (len > 10)
|
||||||
|
len = 10;
|
||||||
|
if (len != 0)
|
||||||
|
memcpy(g_prefix, prefix, len);
|
||||||
|
g_prefix[len] = 0;
|
||||||
|
} // setPrefix
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
/** Selects background/foreground colors for the message depending on
|
/** Selects background/foreground colors for the message depending on
|
||||||
@ -155,9 +167,9 @@ void Log::printMessage(int level, const char *component, const char *format,
|
|||||||
int index = 0;
|
int index = 0;
|
||||||
int remaining = MAX_LENGTH;
|
int remaining = MAX_LENGTH;
|
||||||
|
|
||||||
if (!m_prefix.empty())
|
if (strlen(g_prefix) != 0)
|
||||||
{
|
{
|
||||||
index += snprintf(line+index, remaining, "%s ", m_prefix.c_str());
|
index += snprintf(line+index, remaining, "%s ", g_prefix);
|
||||||
remaining = MAX_LENGTH - index > 0 ? MAX_LENGTH - index : 0;
|
remaining = MAX_LENGTH - index > 0 ? MAX_LENGTH - index : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,14 +73,11 @@ private:
|
|||||||
int m_level;
|
int m_level;
|
||||||
};
|
};
|
||||||
static Synchronised<std::vector<struct LineInfo> > m_line_buffer;
|
static Synchronised<std::vector<struct LineInfo> > m_line_buffer;
|
||||||
|
|
||||||
/** <0 if no buffered logging is to be used, otherwise this is
|
/** <0 if no buffered logging is to be used, otherwise this is
|
||||||
** the maximum number of lines the buffer should hold. */
|
** the maximum number of lines the buffer should hold. */
|
||||||
static size_t m_buffer_size;
|
static size_t m_buffer_size;
|
||||||
|
|
||||||
/** An optional prefix to be printed. */
|
|
||||||
static std::string m_prefix;
|
|
||||||
|
|
||||||
static void setTerminalColor(LogLevel level);
|
static void setTerminalColor(LogLevel level);
|
||||||
static void resetTerminalColor();
|
static void resetTerminalColor();
|
||||||
static void writeLine(const char *line, int level);
|
static void writeLine(const char *line, int level);
|
||||||
@ -152,7 +149,8 @@ public:
|
|||||||
} // disableColor
|
} // disableColor
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
/** Sets a prefix to be printed before each line. To disable the prefix,
|
/** Sets a prefix to be printed before each line. To disable the prefix,
|
||||||
* set it to "". */
|
* set it to "", max length of prefix is 10, if larger than that the
|
||||||
static void setPrefix(const std::string &prefix) { m_prefix = prefix; }
|
* remaining characters are ignored. */
|
||||||
|
static void setPrefix(const char* prefix);
|
||||||
}; // Log
|
}; // Log
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user