Skip to content

Commit 51c9cba

Browse files
committed
refactor logging: make log header-only, rename log_msg to logf, make thread-safe
1 parent 680f5bf commit 51c9cba

File tree

5 files changed

+48
-60
lines changed

5 files changed

+48
-60
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ set(SOURCES
4949
src/Junction.cpp
5050
src/Lane.cpp
5151
src/LaneSection.cpp
52-
src/Log.cpp
5352
src/Mesh.cpp
5453
src/OpenDriveMap.cpp
5554
src/RefLine.cpp

include/Log.h

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
2+
#include "Utils.hpp"
23

3-
#include <functional>
4+
#include <atomic>
5+
#include <cstdio>
6+
#include <string>
47

58
namespace odr
69
{
@@ -12,11 +15,45 @@ enum LogLevel
1215
Error = 2
1316
};
1417

15-
const char* log_level_to_string(const LogLevel level);
18+
inline const char* log_level_to_string(const LogLevel level)
19+
{
20+
switch (level)
21+
{
22+
case LogLevel::Info:
23+
return "INFO";
24+
case LogLevel::Warn:
25+
return "WARN";
26+
case LogLevel::Error:
27+
return "ERROR";
28+
default:
29+
return "UNKNOWN";
30+
}
31+
}
32+
33+
using LogFunction = void (*)(LogLevel, const char*);
1634

17-
using LogFunction = std::function<void(const LogLevel level, const char* message)>;
35+
inline void default_log_function(LogLevel lvl, const char* msg)
36+
{
37+
std::fprintf(stderr, "[%s] %s\n", log_level_to_string(lvl), msg);
38+
}
1839

19-
void set_log_callback(LogFunction log_function);
20-
void log_msg(const LogLevel level, const char* format, ...);
40+
inline std::atomic<LogFunction> g_log_function{&default_log_function};
41+
42+
inline void set_log_callback(LogFunction log_function)
43+
{
44+
g_log_function.store(log_function, std::memory_order_relaxed);
45+
}
46+
47+
inline void log(LogLevel lvl, const char* msg)
48+
{
49+
g_log_function.load(std::memory_order_relaxed)(lvl, msg);
50+
}
51+
52+
template<class... Args>
53+
inline void logf(LogLevel lvl, const char* fmt, Args&&... args)
54+
{
55+
std::string s = string_format(fmt, std::forward<Args>(args)...);
56+
log(lvl, s.c_str());
57+
}
2158

22-
} // namespace odr
59+
} // namespace odr

include/Utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
#define ODR_CHECK(expr, msg) \
1717
{ \
1818
if (!(expr)) \
19-
log_msg(LogLevel::Warn, "[%s] check failed: %s", __FUNCTION__, msg); \
19+
logf(LogLevel::Warn, "[%s] check failed: %s", __FUNCTION__, msg); \
2020
}
2121

2222
#define ODR_CHECK_AND_REPAIR(check_expr, msg, repair_expr) \
2323
{ \
2424
if (!(check_expr)) \
2525
{ \
26-
log_msg(LogLevel::Warn, "[%s] check failed: %s", __FUNCTION__, msg); \
26+
logf(LogLevel::Warn, "[%s] check failed: %s", __FUNCTION__, msg); \
2727
repair_expr; \
2828
} \
2929
}

src/Log.cpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/OpenDriveMap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
6868
{
6969
this->xml_parse_result = this->xml_doc.load_file(xodr_file.c_str());
7070
if (!this->xml_parse_result)
71-
log_msg(LogLevel::Error, "%s", this->xml_parse_result.description());
71+
logf(LogLevel::Error, "%s", this->xml_parse_result.description());
7272

7373
const pugi::xml_node odr_node = this->xml_doc.child("OpenDRIVE");
7474

@@ -305,7 +305,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
305305
}
306306
else
307307
{
308-
log_msg(LogLevel::Error, "Could not parse %s", geometry_type.c_str());
308+
logf(LogLevel::Error, "Could not parse %s", geometry_type.c_str());
309309
continue;
310310
}
311311

@@ -370,7 +370,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
370370
/* check for lateralProfile shape - not implemented yet */
371371
if (road_node.child("lateralProfile").child("shape"))
372372
{
373-
log_msg(LogLevel::Error, "Lateral Profile Shape not supported");
373+
logf(LogLevel::Error, "Lateral Profile Shape not supported");
374374
}
375375
}
376376

0 commit comments

Comments
 (0)