libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNFileMessenger.cpp
Go to the documentation of this file.
1 /* Copyright 2009-2014 Orange Labs, INSA-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNFileMessenger.cpp
19  * \author Guillaume ETIEVENT, Yann LEYDIER
20  */
21 
22 #include <CRNException.h>
23 #include <CRNIO/CRNFileMessenger.h>
24 #include <iostream>
25 #include <CRNi18n.h>
26 
27 using namespace crn;
28 
34 FileMessenger::FileMessenger(const Path& filePath): _filePath("")
35 {
36  //Try opening file
37  _log.open(filePath.CStr());
38  if (!_log.is_open()) {
39  throw ExceptionIO("Error while opening log file at " + StringUTF8(filePath));
40  }
41  _filePath = filePath;
42 
43  DebugPrefix = String(U"[libcrn ") + _("DEBUG") + String(U"] ");
44  VerbosePrefix = String(U"[libcrn ") + _("VERBOSE") + String(U"] ");
45  WarningPrefix = String(U"[libcrn ") + _("WARNING") + String(U"] ");
46  ErrorPrefix = String(U"[libcrn ") + _("ERROR") + String(U"] ");
47 }
48 
51 {
52  if (_log.is_open()) {
53  _log.close();
54  }
55 }
56 
59 {
60  if (_log.is_open()) {
61  _log << DebugPrefix.CStr() << msg.CStr() << std::endl;
62  }
63 }
64 
67 {
68  if (_log.is_open()) {
69  _log << VerbosePrefix.CStr() << msg.CStr() << std::endl;
70  }
71 }
72 
75 {
76  if (_log.is_open()) {
77  _log << WarningPrefix.CStr() << msg.CStr() << std::endl;
78  }
79 }
80 
83 {
84  if (_log.is_open()) {
85  _log << ErrorPrefix.CStr() << msg.CStr() << std::endl;
86  }
87 }
88 
89 
String WarningPrefix
Definition: CRNMessenger.h:53
#define _(String)
Definition: CRNi18n.h:51
const char * CStr() const
Conversion to UTF8 cstring.
Definition: CRNString.cpp:167
A UTF32 character string class.
Definition: CRNString.h:61
std::ofstream _log
Stream to write in.
const char * CStr() const noexcept
Conversion to UTF8 cstring.
virtual void PrintError(const String &msg) override
Prints an error message.
A convenience class for file paths.
Definition: CRNPath.h:39
virtual ~FileMessenger() override
Destructor.
FileMessenger(const Path &filePath)
Constructor with filename.
String DebugPrefix
Definition: CRNMessenger.h:53
virtual void PrintDebug(const String &msg) override
Prints a debug message.
virtual void PrintWarning(const String &msg) override
Prints a warning message.
String ErrorPrefix
Definition: CRNMessenger.h:53
virtual void PrintVerbose(const String &msg) override
Prints a message.
String VerbosePrefix
Definition: CRNMessenger.h:53
A character string class.
Definition: CRNStringUTF8.h:49
Path _filePath
Path of the file to be written.
I/O error.
Definition: CRNException.h:179