
please wait
flog
in terms of the LOG_INFO macro (assume some other work is done inside flog). But since the preprocessor expands this macro where used, __LINE__ evaluates to the line inside flog (Line 25), instead of where flog actually gets used (Line 52). FLOG_INFO
in Main.cpp but this is would be a hassle for every Logger user to define.
|
|
|
|
[Date] ConsoleApplication1.cpp main Line-51 [INFO] Hello Alice! [Date] ConsoleApplication1.cpp flog Line-25 [ERROR] Hello Bob! [Date] ConsoleApplication1.cpp main Line-53 [INFO] Hello Bob! |
__FILE__
and __LINE__
macros expand to the file name or line number of the code location where they are expanded!
|
|
|
|
|
|
Log message received from file "c:\repos\consoleapplication40\main.c" at line #8: "foo" Log message received from file "c:\repos\consoleapplication40\main.c" at line #9: "bar" Log message received from file "c:\repos\consoleapplication40\main.c" at line #10: "baz" |
kigar64551 wrote: |
---|
But you can still use them inside a macro definition. And that macro definition can, very well, be inside a "shared" header file. There is no need to re-define (copy) our "helper" macro in every source file where logging is used! |
struct Foo
in this case) decides it wants to implement its own log-like call on top of the helper macros, see Foo::flog
, then wherever Foo::flog gets called (e.g., Main.cpp, Line-52), the logged line number is incorrect.
|
|
|
|
Foo::Report
, or if they need to redefine another set of helper macros (as I did with #define FLOG_INFO
).One unfortunate consequence of the use of helper macros, instead of an object-oriented approach, is that if another class (struct Foo in this case) decides it wants to implement its own log-like call on top of the helper macros, see Foo::flog, then wherever Foo::flog gets called (e.g., Main.cpp, Line-52), the logged line number is incorrect. |
|
|
|
|
|
|
|
|
kigar64551 wrote: |
---|
#ifdef CUSTOM_LOGGER #ifdef CUSTOM_LOGGER # define LOG(TEXT) CUSTOM_LOGGER(__FILE__, __LINE__, (TEXT)) #else # define LOG(TEXT) logger_default(__FILE__, __LINE__, (TEXT)) #endif ... #define CUSTOM_LOGGER my_special_logger include "logging.h" static void my_special_logger(const char *const file_name, const int line_no, const char *const text) { const char *custom_message = /* ... */; logger_default(file_name, line_no, custom_message); } |