Skip to content

Commit e56c5a5

Browse files
committed
refactor: put log functions in odr::log namespace, add and use log::info, warn and error convenience functions
1 parent 178ae7d commit e56c5a5

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

include/Log.hpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,78 @@
88
namespace odr
99
{
1010

11-
enum LogLevel
11+
namespace log
12+
{
13+
14+
enum Level
1215
{
1316
Info = 0,
1417
Warn = 1,
1518
Error = 2
1619
};
1720

18-
inline const char* log_level_to_string(const LogLevel level)
21+
inline const char* level_to_string(const Level level)
1922
{
2023
switch (level)
2124
{
22-
case LogLevel::Info:
25+
case Level::Info:
2326
return "INFO";
24-
case LogLevel::Warn:
27+
case Level::Warn:
2528
return "WARN";
26-
case LogLevel::Error:
29+
case Level::Error:
2730
return "ERROR";
2831
default:
2932
return "UNKNOWN";
3033
}
3134
}
3235

33-
using LogFunction = void (*)(LogLevel, const char*);
36+
using LogFunction = void (*)(Level, const char*);
3437

35-
inline void default_log_function(LogLevel lvl, const char* msg)
38+
inline void default_log_function(Level lvl, const char* msg)
3639
{
37-
std::fprintf(stderr, "[%s] %s\n", log_level_to_string(lvl), msg);
40+
std::fprintf(stderr, "[%s] %s\n", level_to_string(lvl), msg);
3841
}
3942

4043
inline std::atomic<LogFunction> g_log_function{&default_log_function};
41-
inline std::atomic<LogLevel> g_log_level{LogLevel::Warn};
44+
inline std::atomic<Level> g_log_level{Level::Warn};
4245

43-
inline void set_log_callback(LogFunction log_function)
46+
inline void set_callback(LogFunction log_function)
4447
{
4548
g_log_function.store(log_function, std::memory_order_relaxed);
4649
}
4750

48-
inline void set_log_level(LogLevel lvl)
51+
inline void set_level(Level lvl)
4952
{
5053
g_log_level.store(lvl, std::memory_order_relaxed);
5154
}
5255

53-
inline void log(LogLevel lvl, const char* msg)
56+
template<class... Args>
57+
void log(Level lvl, const char* fmt, Args&&... args)
5458
{
5559
if (lvl < g_log_level.load(std::memory_order_relaxed))
5660
return;
57-
g_log_function.load(std::memory_order_relaxed)(lvl, msg);
61+
std::string s = strfmt(fmt, std::forward<Args>(args)...);
62+
g_log_function.load(std::memory_order_relaxed)(lvl, s.c_str());
5863
}
5964

6065
template<class... Args>
61-
void logf(LogLevel lvl, const char* fmt, Args&&... args)
66+
void info(const char* fmt, Args&&... args)
6267
{
63-
std::string s = strfmt(fmt, std::forward<Args>(args)...);
64-
log(lvl, s.c_str());
68+
log(Level::Info, fmt, std::forward<Args>(args)...);
69+
}
70+
71+
template<class... Args>
72+
void warn(const char* fmt, Args&&... args)
73+
{
74+
log(Level::Warn, fmt, std::forward<Args>(args)...);
6575
}
6676

77+
template<class... Args>
78+
void error(const char* fmt, Args&&... args)
79+
{
80+
log(Level::Error, fmt, std::forward<Args>(args)...);
81+
}
82+
83+
} // namespace log
84+
6785
} // namespace odr

include/Utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ template<class... Args>
2121
inline void check(const bool ok, const char* fmt, Args&&... args)
2222
{
2323
if (!ok)
24-
logf(LogLevel::Warn, fmt, std::forward<Args>(args)...);
24+
log::warn(fmt, std::forward<Args>(args)...);
2525
}
2626

2727
template<class Repair, class... Args>
2828
inline void check_and_repair(const bool ok, Repair&& repair, const char* fmt, Args&&... args)
2929
{
3030
if (!ok)
3131
{
32-
logf(LogLevel::Warn, fmt, std::forward<Args>(args)...);
32+
log::warn(fmt, std::forward<Args>(args)...);
3333
std::forward<Repair>(repair)();
3434
}
3535
}

src/OpenDriveMap.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
7575
{
7676
this->xml_parse_result = this->xml_doc.load_file(xodr_file.c_str());
7777
if (!this->xml_parse_result)
78-
logf(LogLevel::Error, "%s", this->xml_parse_result.description());
78+
log::error("Error parsing xml: %s", this->xml_parse_result.description());
7979

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

@@ -156,6 +156,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
156156
}
157157
}
158158

159+
odr::check(odr_node.child("road"), "No roads found");
159160
for (const pugi::xml_node road_node : odr_node.children("road"))
160161
{
161162
/* make road */
@@ -329,7 +330,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
329330
}
330331
else
331332
{
332-
logf(LogLevel::Error, "Could not parse %s", geometry_type.c_str());
333+
log::error("Could not parse %s", geometry_type.c_str());
333334
continue;
334335
}
335336

@@ -396,7 +397,7 @@ OpenDriveMap::OpenDriveMap(const std::string& xodr_file,
396397
/* check for lateralProfile shape - not implemented yet */
397398
if (road_node.child("lateralProfile").child("shape"))
398399
{
399-
logf(LogLevel::Error, "Lateral Profile Shape not supported");
400+
log::error("Lateral Profile Shape not supported");
400401
}
401402
}
402403

0 commit comments

Comments
 (0)