overloading a macro ?

Hi

I'm writing a simple logger that is basically just a wrapper around the std c libs 'printf' function, that appends some additioinal information on to the string argument.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once

#include <cstdio>
#include <string>

#include "Systems\Debug\MyDebugSystem.h"
#include "MySingleton.h"

#define DEBUG_LOG(str, ...) MyDebugLoggerSystem::Instance().Log(str, __VA_ARGS__)

class MyDebugLoggerSystem : public MyDebugSystem, public MySingleton<MyDebugLoggerSystem>
{
   public:
      bool Init();

      template <class... Params>
      void Log(std::string msg, Params... p)
      {
         msg.insert(0,"LOG <"+std::string(__FILE__)+", "+std::string(__TIME__)+"> : ");
         msg.append("\n");
         std::printf(msg.c_str(), p...);
      }




};



This all works fine when i call DEBUG_LOG("something %d", 3)

the problem is that i also want to be able to call it with just a string, like:
DEBUG_LOG("something")

I'm not sure how i should go about this.

Does anybody know of some "macro magic" to make this work ?
(like, maybe overload the DEBUG_LOG macro and parse NULL for the second parameter to Log(), or something along those lines)

Thanks for reading :)
Why even use a macro?
1
2
3
4
template <typename... T>
void DEBUG_LOG(T &&... p){
    MyDebugLoggerSystem::Instance().Log(std::forward<T>(p)...);
}

By the way, could I maybe convince you to implement Log() differently?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private:
static void write(std::ostream &stream){}

template <typename T, typename... Params>
static void write(std::ostream &stream, T &&x, Params &&... p){
    stream << x;
    write(stream, std::forward<Params>(p)...);
}

public:
template <typename... Params>
void Log(const std::string &msg, Params &&... p)
{
    //Note: __FILE__ and __TIME__ will write the filename that contains Log(),
    //      and the time at which Log() was compiled. NOT the filename that
    //      contains the function that called Log(), nor the time at which Log()
    //      was called.
    std::cout << "LOG <" << __FILE__ << ", " << __TIME__ << "> : " << msg << std::endl;
    write(std::cout, std::forward<Params>(p)...);
}

void example(){
    DEBUG_LOG("hello!", 14, " ", 3.141592);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <boost/format.hpp>
#include <complex>

namespace utility
{
    // http://en.cppreference.com/w/cpp/language/fold (C++17)
    // http://www.boost.org/doc/libs/1_66_0/libs/format/doc/format.html
    template < typename... ARGS > std::ostream& log( std::ostream& stm, std::string fmt, ARGS&&... args )
    { return stm << ( boost::format(fmt) % ... % args ) ; } 

    template < typename... ARGS > std::ostream& log( std::string fmt, ARGS&&... args )
    { return log( std::clog, fmt, std::forward<ARGS>(args)... ) ; }
}

int main()
{
    utility::log( "hello" ) << '\n' ;
    utility::log( "hello %s! |%=+12d| %012.2f   complex%.1f\n", "again", 345, -56.789, std::complex<double>(1.0,2.0) ) ;
}

http://coliru.stacked-crooked.com/a/6998342f69f409b1
Nice. I thought boost::format used the %index% syntax.
Topic archived. No new replies allowed.