简体   繁体   English

我可以在 C++ 的内联 function 中使用 __LINE__ 或 __FILE__ 吗?

[英]Can I use __LINE__ or __FILE__ in inline function in C++?

I faced a problem while implementing the logger.我在实现记录器时遇到了一个问题。

First, I used to LINE and FILE with standard C Macro function like below首先,我曾经使用标准 C 宏 function 像下面这样LINEFILE

// global.h
..
namespace MyLogger {

class Logger
{
    ..
    void _write(int _level, const char* _file, int _line, const char* _fmt, ...);
};
static Logger    logger;

}; // namespace MyLogger

..

// avoid conflict..
#undef error
#undef trace

#define error(_MESSAGE_, ...) _write(LOG_LEVEL_ERROR, (const char*)__FILE__, (int)__LINE__, (const char*)_MESSAGE_, ##__VA_ARGS__)
#define info(_MESSAGE_, ...)  _write(LOG_LEVEL_INFO,  (const char*)__FILE__, (int)__LINE__, (const char*)_MESSAGE_, ##__VA_ARGS__)
#define trace(_MESSAGE_, ...) _write(LOG_LEVEL_TRACE, (const char*)__FILE__, (int)__LINE__, (const char*)_MESSAGE_, ##__VA_ARGS__)
..

// main.cpp

using namespace MyLogger;

// using like this
logger.error("- log error");
logger.info("- log info %s", "test 1");
..

In fact, it seems to work well.事实上,它似乎运作良好。 However, the problem occurred while using openv.但是,在使用 openv 时出现了问题。

error and trace seem to conflicted with error and trace in opencv . errortrace似乎与opencv中的errortrace冲突。

error: expected primary-expression before 'int' [build] CV_EXPORTS CV_NORETURN void error(int _code, const String& _err, const char* _func, const char* _file, int _line);错误:“int”之前的预期主表达式 [build] CV_EXPORTS CV_NORETURN void error(int _code, const String& _err, const char* _func, const char* _file, int _line);

So I'm thinking about other methods using inline functions, not macros.所以我正在考虑使用内联函数而不是宏的其他方法。

// global.h
..
    //void _write(int _level, const char* _file, int _line, const char* _fmt, ...);
    static inline void error(/* Is it possible to toss __LINE__ or __FILE__ ? */);
    static inline void info(/* .. */);
    static inline void trace(/* .. */);
..

// main.cpp
logger::error("- log error");
logger::info("%d %c %f, 1, 'A', '0.1');

Can I know the line or file of the location where the log was output through a method other than a macro?能否通过宏以外的方法知道日志为output所在位置的行或文件?

No , inline functions/methods won't work the same as macro in this context.,在这种情况下, inline函数/方法与宏的工作方式不同。 Macros are simply text replacement, so the __LINE__ and __FILE__ will give accurate results.宏只是文本替换,因此__LINE____FILE__将给出准确的结果。 So macros are your only choice ;所以宏是你唯一的选择 see if you can name them better to avoid conflicts.看看您是否可以更好地命名它们以避免冲突。

However the inline functions are systematically compiled subunits.然而, inline函数是系统编译的子单元。 So the line number and the file names will always be same that of those functions where it resides.因此,行号和文件名将始终与它所在的那些函数相同。

Refer: Inline functions vs Preprocessor macros参考: 内联函数与预处理器宏

There are 3 stages in which C++ becomes a running program. C++ 成为运行程序有 3 个阶段。

  1. Preprocessor预处理器
  2. Compiler编译器
  3. Linker Linker

__LINE__ and __FILE__ are processed by the preprocessor (stage 1). __LINE____FILE__由预处理器处理(阶段 1)。 However, it's the compiler that decides about inlining (stage 2).但是,决定内联(第 2 阶段)的是编译器。

Other languages like C# have this in the compiler stage: [CallerMemberName] , [CallerFilePath] and [CallerLineNumber] [MSDN] but I am don't think this exists in C++ at the moment. C# 等其他语言在编译器阶段有这个: [CallerMemberName][CallerFilePath][CallerLineNumber] [MSDN]但我不认为这存在于 C++ 中。

How about this:这个怎么样:

#define log_error(_MESSAGE_, ...) \
    logger._write( \
        LOG_LEVEL_ERROR, \
        (const char*)__FILE__, \
        (int)__LINE__, \
        (const char*)_MESSAGE_, \
        ##__VA_ARGS__)

There is no much difference between logger.error and log_error . logger.errorlog_error之间没有太大区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM