cout / cerr包装器ostream,只需很少的努力

时间:2016-04-10 15:02:32

标签: c++ operators ostream variadic-functions

我想更好地学习C ++(目前我的C ++仅限于它的C子集*咳嗽* ...),因此我决定尝试使用C ++ -ify"我从C到C ++的一个有用的日志记录功能,我认为最好用代码解释:

#include <stdarg.h>
#include <stdio.h>

enum msg_type {
    LOG_DBG,
    LOG_INF,
    LOG_WRN,
    LOG_ERR
};

int printf_log(enum msg_type mt, const char *fmt_string, ...)
{
    va_list args;
    va_start(args, fmt_string);

    switch (mt) {
        case LOG_DBG:
            printf("[D] ");
            break;
        case LOG_INF:
            printf("[I] ");
            break;
        case LOG_WRN:
            printf("[W] ");
            break;
        case LOG_ERR:
            printf("[E] ");
            break;
        default:
            break;
    }

    int res = vprintf(fmt_string, args);

    va_end(args);
    return res;
}

int main()
{
    int i = 0;
    printf_log(LOG_DBG, "i is %d\n", i);
    i++;
    printf_log(LOG_INF, "i is %d\n", i);
    i++;
    printf_log(LOG_WRN, "i is %d\n", i);
}

这应输出:

[D] i is 0
[I] i is 1
[W] i is 2

我对C ++版本的想法就是:

#include <log.h>

int main()
{
    int i = 0;
    log::log(log::dbg)<<"i is " << i << "\n";
    i++;
    log::log(log::inf)<< "i is " << i << "\n";
    i++;
    log::log(log::wrn)<< "i is " << i << "\n";
}

具有相同的结果。

我特别想避免对参数进行任何解析(可能除了log::inf或类似),但只是让它们直接传递到cout

但我并不知道从哪里开始,所有与此有关的事情要么想要更多的C ++知识,要么希望你实现整个自己的streambuf或类似的。

我的想法基本相同,所以我尽了最大努力,这是我在互联网上使用不同资源制作的代码:

#include <iostream>

using std::cout;
class Log {

    public:
        enum msg_type {
            dbg =1,
            inf,
            wrn,
            err
        };

        Log(enum msg_type mt)
        {
            switch (mt) {
            case dbg:
                cout << "[D] ";
                break;
            case inf:
                cout << "[I] ";
                break;
            case wrn:
                cout << "[W] ";
                break;
            case err:
                cout << "[E] ";
                break;
            default:
                break;
            }
        }

        template<typename T>
        const Log& operator<<(const T& t)
        {

            std::cout << t;
            return *this;
        }
};

int main()
{
    int i = 0;
    Log(Log::dbg)<< "i is " << i++ << "\n";
    Log(Log::inf)<< "i is " << i++ << "\n";
    Log(Log::inf)<< "i is " << i++ << "\n";
}

显然它不起作用,但我不知道错误信息会告诉我什么。

G ++:

main.cc: In function ‘int main()’:
main.cc:46:34: error: passing ‘const Log’ as ‘this’ argument discards qualifiers [-fpermissive]
      Log(Log::dbg)<< "i is " << i++ << "\n";
                                  ^
main.cc:35:14: note:   in call to ‘const Log& Log::operator<<(const T&) [with T = int]’
   const Log& operator<<(const T& t)
              ^
main.cc:46:40: error: passing ‘const Log’ as ‘this’ argument discards qualifiers [-fpermissive]
      Log(Log::dbg)<< "i is " << i++ << "\n";
                                        ^
main.cc:35:14: note:   in call to ‘const Log& Log::operator<<(const T&) [with T = char [2]]’
   const Log& operator<<(const T& t)
              ^

锵:

main.cc:46:30: error: invalid operands to binary expression ('const Log' and 'int')
        Log(Log::dbg)<< "i is " << i++ << "\n";
        ~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~
main.cc:35:14: note: candidate function not viable: 'this' argument has type
      'const Log', but method is not marked const
                const Log& operator<<(const T& t)
                           ^
1 error generated.

最简单的方法当然是做一个宏来替换我记录的任何事件&#34; fake-ostream&#34;用例如。 cout << [I] <<但这远不那么灵活。

有没有人对如何正确地做到这一点有任何想法,我可以提示一下这些资源吗?

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

const对象是常量,因此您只能调用常量成员函数。

你说通过在声明之后添加class Log { public: ... template<typename T> const Log& operator<<(const T& t) const // ^^^^^ // Note const keyword here { ... } ... }; 关键字,成员函数是不变的。

所以你的课应该是这样的(缩写):

cin