cdoDebugOutput.h 4.1 KB
Newer Older
Oliver Heidmann's avatar
Oliver Heidmann committed
1
2
3
4
5
6
#ifndef DEBUG_SWITCHES_H
#define DEBUG_SWITCHES_H
#include <iostream>
#include <sstream>

#include <fstream>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <string.h>

namespace CdoLog
{
    void StdOut(std::stringstream &message);

    template <typename ...T>
    void  expand(std::stringstream &p_message, T&& ...args)
    {
           //for showing that the dummy array is never used
           using expander = int[];
           //creating dummy array with expanding the parameter pack in its
           //initializer list while writing each element of the pack into message
           expander{0,(void(p_message << std::forward<T>(args)),0)...};
           p_message << std::endl;
    }

    template <typename ...T>
    void StdOut(T&& ...args)
    {
        std::stringstream message;
        expand(message, args...);
        std::cout << message.str();
    }
 
    template <typename ...T>
    void StdErr(T&& ...args)
    {
        std::stringstream message;
        expand(message, args...);
        std::cout << message.str();
    }



   
}
Oliver Heidmann's avatar
Oliver Heidmann committed
44
45
46
47
48

namespace CdoDebug
{
    extern int  PSTREAM;
    extern bool PROCESS;
49
50
    extern bool PIPE;
    extern int ARGUMENT;
Oliver Heidmann's avatar
Oliver Heidmann committed
51
52
    extern std::string outfile;
    extern bool print_to_seperate_file;
53
54
    extern std::fstream outfile_stream;
    extern int PTHREAD;
Oliver Heidmann's avatar
Oliver Heidmann committed
55

56
57
58
59
    std::string get_padding(const char *p_func);

    void CdoStartMessage();
    void CdoEndMessage();
Oliver Heidmann's avatar
Oliver Heidmann committed
60

61
62
63
64
65
66
67
68
69
   namespace{
        void printMessage(std::stringstream &p_message)
        {
            if(!print_to_seperate_file)
            {
                std::cout << p_message.str();
            }
            else 
            {
Oliver Heidmann's avatar
Oliver Heidmann committed
70
71
72
73
74
75
                outfile_stream <<  p_message.str();
            }
        }
    }


76
   
Oliver Heidmann's avatar
Oliver Heidmann committed
77
    template <typename ...T>
78
    void Message_ (const char * p_func, T&& ...args)
Oliver Heidmann's avatar
Oliver Heidmann committed
79
    {
80
81
82
83
        std::stringstream message;
        message << p_func <<": " << get_padding(p_func);
        CdoLog::expand(message, args...);
        printMessage(message);
Oliver Heidmann's avatar
Oliver Heidmann committed
84
85
    }

86
    
Oliver Heidmann's avatar
Oliver Heidmann committed
87
    template <typename ...T>
88
    void Warning_(T&& ...args)
Oliver Heidmann's avatar
Oliver Heidmann committed
89
90
    {
        std::stringstream message;
91
92
93
        message << "Warning: ";
        CdoLog::expand(message, args...);
        std::cout << message.str();
Oliver Heidmann's avatar
Oliver Heidmann committed
94
    }
95
96


Oliver Heidmann's avatar
Oliver Heidmann committed
97
98
99
100
101
102
103
104
105
106
}

namespace CdoError{
    static int _ExitOnError = 1;

    template <typename ...T>
    void Error_(const char* p_file, const int p_line, const char* caller, T&& ...args)
    {
          std::stringstream message;
          message << "Error in: " << p_file << ":" << p_line << " ";
107
108
          CdoLog::expand(message, args...);
          CdoLog::StdOut(message);
Oliver Heidmann's avatar
Oliver Heidmann committed
109
110
111
112
113
          if ( CdoError::_ExitOnError )
          {
              exit(EXIT_FAILURE);
          }
    }
114
    
Oliver Heidmann's avatar
Oliver Heidmann committed
115
116
117
118
119
120
121
    template <typename ...T>
    void SysError_(const char* p_file, const int p_line,  const char* p_func, T&& ...args)
    {
        int saved_errno = errno;
        std::stringstream message;
        message << "SysError in:" << p_file << std::endl;
        message << "    " << "in function: p_func ,line: " << p_line << std::endl;
122
123
        CdoLog::StdOut(message, args...);
        CdoLog::StdOut(message);
Oliver Heidmann's avatar
Oliver Heidmann committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        if(saved_errno)
        {
            errno = saved_errno;
            perror("Sytem error message");
        }
        exit(EXIT_FAILURE);
    }
}
#if  defined  WITH_CALLER_NAME
#  define  SYS_ERROR(...)    CdoError::SysError_( __FILE__ , __LINE__ , __func__ , __VA_ARGS__)
#  define    ERROR_C(...)    CdoError::Error_( __FILE__ , __LINE__ ,  caller, __VA_ARGS__)
#  define    ERROR(...)    CdoError::Error_( __FILE__ , __LINE__ , __func__ , __VA_ARGS__)
#  define   WARNING(...)    CdoError::Warning_( __func__ , __VA_ARGS__)
#  define  MESSAGE_C(...)    CdoDebug::Message_(   caller , __VA_ARGS__)
#  define   MESSAGE(...)    CdoDebug::Message_( __func__ , __VA_ARGS__)
#else
#  define  SYS_ERROR(...)    CdoError::SysError_(__FILE__, __LINE__,"", __VA_ARGS__)
#  define    ERROR_C(...)    CdoError::Error_(__FILE__, __LINE__,"", __VA_ARGS__)
#  define    ERROR(...)      CdoError::Error_(__FILE__, __LINE__,"", __VA_ARGS__)
#  define   WARNING(...)     CdoError::Warning_(__VA_ARGS__)
#  define  MESSAGE_C(...)    CdoDebug::Message_(__VA_ARGS__)
#  define   MESSAGE(...)     CdoDebug::Message_(__func__,__VA_ARGS__)
#endif

#endif