Commit 90950688 authored by Oliver Heidmann's avatar Oliver Heidmann
Browse files

added function for printing to stdout and stderr for consistency, moved...

added function for printing to stdout and stderr for consistency, moved function between output namespaces
parent 26651ced
#include "cdoDebugOutput.h" #include "cdoDebugOutput.h"
namespace CdoLog
{
void StdOut(std::stringstream & p_message)
{
std::cout << p_message.str();
}
}
namespace CdoDebug
{
int PTHREAD;
int PSTREAM;
bool PROCESS;
bool PIPE;
int ARGUMENT;
namespace CdoDebug{ std::string outfile = "";
bool print_to_seperate_file = false;
std::fstream outfile_stream;
#if defined(DEBUG_PSTREAM) || defined(DEBUG) std::string
int PSTREAM = 1; get_padding(const char *p_func)
#else {
int PSTREAM = 0; size_t len = strlen(p_func);
#endif
#if defined(DEBUG_PROCESS) || defined(DEBUG) return std::string(30 - len, ' ');
bool PROCESS = true; }
#else
bool PROCESS = false;
#endif
std::string outfile = ""; void
bool print_to_seperate_file = false; CdoStartMessage()
} {
std::stringstream message;
outfile_stream = std::fstream(outfile, std::fstream::in | std::fstream::app);
message << std::string(30, ' ') << " == CDO Start ==" << std::endl;
printMessage(message);
}
void
CdoEndMessage()
{
std::stringstream message;
message << std::string(30, ' ') << " == CDO End ==" << std::endl;
printMessage(message);
outfile_stream.close();
}
}
...@@ -4,52 +4,96 @@ ...@@ -4,52 +4,96 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#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();
}
}
namespace CdoDebug namespace CdoDebug
{ {
extern int PSTREAM; extern int PSTREAM;
extern bool PROCESS; extern bool PROCESS;
extern bool PIPE;
extern int ARGUMENT;
extern std::string outfile; extern std::string outfile;
extern bool print_to_seperate_file; extern bool print_to_seperate_file;
extern std::fstream outfile_stream;
extern int PTHREAD;
namespace{ std::string get_padding(const char *p_func);
void printMessageToFile(std::stringstream &p_message)
{ void CdoStartMessage();
if(!outfile.empty()){ void CdoEndMessage();
std::fstream outfile_stream(outfile,std::fstream::in | std::fstream::app );
namespace{
void printMessage(std::stringstream &p_message)
{
if(!print_to_seperate_file)
{
std::cout << p_message.str();
}
else
{
outfile_stream << p_message.str(); outfile_stream << p_message.str();
} }
} }
} }
template <typename ...T> template <typename ...T>
void Message_ (std::stringstream &p_message, T&& ...args) void Message_ (const char * p_func, T&& ...args)
{ {
//for showing that the dummy array is never used std::stringstream message;
using expander = int[]; message << p_func <<": " << get_padding(p_func);
//creating dummy array with expanding the parameter pack in its CdoLog::expand(message, args...);
//initializer list while writing each element of the pack into message printMessage(message);
expander{0,(void(p_message << std::forward<T>(args)),0)...};
p_message << std::endl;
} }
template <typename ...T> template <typename ...T>
void Message_ (const char * p_func, T&& ...args) void Warning_(T&& ...args)
{ {
std::stringstream message; std::stringstream message;
message << p_func <<": "; message << "Warning: ";
Message_(message, args...); CdoLog::expand(message, args...);
if(print_to_seperate_file) std::cout << message.str();
{
printMessageToFile(message);
}
else
{
std::cout << message.str();
}
} }
} }
namespace CdoError{ namespace CdoError{
...@@ -60,22 +104,14 @@ namespace CdoError{ ...@@ -60,22 +104,14 @@ namespace CdoError{
{ {
std::stringstream message; std::stringstream message;
message << "Error in: " << p_file << ":" << p_line << " "; message << "Error in: " << p_file << ":" << p_line << " ";
CdoDebug::Message_(message, args...); CdoLog::expand(message, args...);
std::cout << message.str(); CdoLog::StdOut(message);
if ( CdoError::_ExitOnError ) if ( CdoError::_ExitOnError )
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
template <typename ...T>
void Warning_(T&& ...args)
{
std::stringstream message;
message << "Warning: ";
CdoDebug::Message_(message, args...);
std::cout << message.str();
}
template <typename ...T> template <typename ...T>
void SysError_(const char* p_file, const int p_line, const char* p_func, T&& ...args) void SysError_(const char* p_file, const int p_line, const char* p_func, T&& ...args)
{ {
...@@ -83,8 +119,8 @@ namespace CdoError{ ...@@ -83,8 +119,8 @@ namespace CdoError{
std::stringstream message; std::stringstream message;
message << "SysError in:" << p_file << std::endl; message << "SysError in:" << p_file << std::endl;
message << " " << "in function: p_func ,line: " << p_line << std::endl; message << " " << "in function: p_func ,line: " << p_line << std::endl;
CdoDebug::Message_(message, args...); CdoLog::StdOut(message, args...);
CdoLog::StdOut(message);
if(saved_errno) if(saved_errno)
{ {
errno = saved_errno; errno = saved_errno;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment