libdigidocpp
log.cpp
Go to the documentation of this file.
1 /*
2  * libdigidocpp
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "log.h"
21 
22 #include "Conf.h"
23 #include "util/File.h"
24 
25 #include <fstream>
26 #include <iomanip>
27 #include <memory.h>
28 #include <stdio.h>
29 
30 using namespace digidoc;
31 
41 std::string Log::format(const char* fmt, ...)
42 {
43  va_list args;
44  va_start(args, fmt);
45  std::string s = formatArgList(fmt, args);
46  va_end(args);
47  return s;
48 }
49 
58 std::string Log::formatArgList(const char* fmt, va_list args)
59 {
60  if(!fmt)
61  return "";
62  std::string result(256, 0);
63  int size = vsnprintf(&result[0], result.size() + 1, fmt, args);
64  if(size == -1)
65  return "";
66  result.resize(size);
67  return result;
68 }
69 
70 void Log::out(LogType type, const char *file, unsigned int line, const char *format, ...)
71 {
72  Conf *conf = Conf::getInstance();
73  if(!conf || conf->getLogLevel() < type)
74  return;
75 
76  std::ostream *o = &std::cout;
77  std::fstream f;
78  if(!conf->getLogFile().empty())
79  {
81  f.open(enc.c_str(), std::fstream::out|std::fstream::app);
82  o = &f;
83  }
84  switch(type)
85  {
86  case ErrorType: *o << "ERROR"; break;
87  case WarnType: *o << "WARN"; break;
88  case InfoType: *o << "INFO"; break;
89  case DebugType: *o << "DEBUG"; break;
90  }
91  *o << " [" << file << ":" << line << "] - ";
92 
93  va_list args;
94  va_start(args, format);
95  *o << formatArgList(format, args).c_str() << "\n";
96  va_end(args);
97 }
98 
99 void Log::dbgPrintfMemImpl(const char *msg, const void *ptr, size_t size, const char *file, int line)
100 {
101  Conf *conf = Conf::getInstance();
102  if(!conf || conf->getLogLevel() < DebugType)
103  return;
104 
105  std::ostream *o = &std::cout;
106  std::fstream f;
107  if(!conf->getLogFile().empty())
108  {
110  f.open(enc.c_str(), std::fstream::out|std::fstream::app);
111  o = &f;
112  }
113 
114  const unsigned char *data = (const unsigned char*)ptr;
115  *o << "DEBUG [" << file << ":" << line << "] - " << msg << " { ";
116  for(size_t i = 0; i < size; ++i)
117  *o << '%' << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << static_cast<int>(data[i]) << ' ';
118  *o << "}:" << size << "\n";
119 }