59570e8eb1ff1e3dd04f3ac2712948f95eb3a030
[platform/upstream/syncevolution.git] / src / syncevo / LogStdout.cpp
1 /*
2  * Copyright (C) 2009 Patrick Ohly <patrick.ohly@gmx.de>
3  * Copyright (C) 2009 Intel Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) version 3.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301  USA
19  */
20
21 #include <syncevo/LogStdout.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <syncevo/declarations.h>
26 SE_BEGIN_CXX
27
28
29 LoggerStdout::LoggerStdout(FILE *file) :
30     m_file(file),
31     m_closeFile(false)
32 {}
33
34 LoggerStdout::LoggerStdout(const std::string &filename) :
35     m_file(fopen(filename.c_str(), "w")),
36     m_closeFile(true)
37 {
38     if (!m_file) {
39         throw std::string(filename + ": " + strerror(errno));
40     }
41 }
42
43 LoggerStdout::~LoggerStdout()
44 {
45     if (m_closeFile) {
46         fclose(m_file);
47     }
48 }
49
50 void LoggerStdout::messagev(FILE *file,
51                             Level msglevel,
52                             Level filelevel,
53                             const char *prefix,
54                             const char *filename,
55                             int line,
56                             const char *function,
57                             const char *format,
58                             va_list args)
59 {
60     if (file &&
61         msglevel <= filelevel) {
62         // TODO: print time
63         fprintf(file, "[%s] ", levelToStr(msglevel));
64         if (prefix) {
65             fprintf(file, "%s: ", prefix);
66         }
67         // TODO: print debugging information, perhaps only in log file
68         vfprintf(file, format, args);
69         // TODO: add newline only when needed, add prefix to all lines
70         fprintf(file, "\n");
71         fflush(file);
72     }
73 }
74
75 void LoggerStdout::messagev(Level level,
76                             const char *prefix,
77                             const char *file,
78                             int line,
79                             const char *function,
80                             const char *format,
81                             va_list args)
82 {
83     messagev(m_file, level, getLevel(),
84              prefix, file, line, function,
85              format, args);
86 }
87
88 SE_END_CXX