* Refactoring and improvements CAmDatabasehandlerMap ( Ticket #87 )
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / include / CAmLog.h
1 /**
2  * Copyright (C) 2012, BMW AG
3  *
4  * This file is part of GENIVI Project AudioManager.
5  *
6  * Contributions are licensed to the GENIVI Alliance under one or more
7  * Contribution License Agreements.
8  *
9  * \copyright
10  * This Source Code Form is subject to the terms of the
11  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
12  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  *
15 * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
16  *
17  * \file CAmLog.h
18  * For further information see http://www.genivi.org/.
19  *
20  */
21
22 #ifndef CAMLOG_H_
23 #define CAMLOG_H_
24
25 #include <iostream>
26 #include <iosfwd>
27 #include <stdio.h>
28 #include <stdexcept>
29 #include <fstream>
30 #include <stdlib.h>
31 #include <sstream>
32 #include <assert.h>
33
34 /**
35  * Implements a basic logging mechanism that can be used to print debug information into a file or to the console.
36  * It can be used either as singleton through the appropriate method getDefaultLog() or as independent instantiated object.
37  * The default initializer sets the console as output for newly created objects.
38  * Example: CAmLogger << "Text"; //to print out through the singleton object directly to the console
39  */
40
41 #define DEFAULT_LOG_FOLDER              "/tmp/"
42 #define DEFAULT_LOGFILE_PREFIX "am_dump_"
43 #define DEFAULT_LOGFILE_EXT     ".log"
44
45 #define DEL( aPointer ) delete aPointer, aPointer = NULL;
46
47 /* */
48 typedef enum { eCAmLogNone = 0, eCAmLogStdout = 1, eCAmLogFile = 2 } eCAmLogType;
49
50 class CAmLog
51 {
52 private:
53         /**
54          * Private classes which usually own (wrap) a stream object. They are responsible for creating and deleting it.
55          */
56         class CAmLogger
57         {
58         protected:
59                 std::ostream* mOutputStream;
60         public:
61                 CAmLogger ():mOutputStream(NULL) {};
62                 virtual ~CAmLogger () { };
63                 virtual void log(const std::string& _s)
64             {
65                 (*mOutputStream) << _s;
66                 mOutputStream->flush();
67             }
68             template <class T>
69             CAmLogger & operator << (const T & t)
70             {
71                 (*mOutputStream) << t;
72                 return (*this);
73             }
74         };
75
76         class CAmFileLogger : public CAmLogger
77         {
78                 std::string mFilename;
79         public:
80                 static void generateLogFilename(std::string &result);
81                 explicit CAmFileLogger(const std::string& _s) : CAmLogger()
82                 {
83                         mFilename = _s;
84                         mOutputStream = new std::ofstream(mFilename.c_str());
85                 }
86                 ~CAmFileLogger();
87         };
88
89         class CAmStdOutLogger : public CAmLogger
90         {
91         public:
92                 CAmStdOutLogger()
93                 {
94                         mOutputStream = &std::cout;
95                 }
96         };
97
98 private:
99         eCAmLogType mLogType;
100     CAmLogger* mLogger;
101
102 protected:
103     void releaseLogger();
104     void instantiateLogger( const eCAmLogType type);
105 public:
106     CAmLog(const eCAmLogType type );
107     CAmLog();
108     ~CAmLog();
109
110     static CAmLog *getDefaultLog();
111
112     void setLogType( const eCAmLogType type);
113     eCAmLogType getLogType() const;
114
115     template <class T>
116     CAmLog & operator << (const T & t)
117     {
118         assert(mLogger!=NULL);
119         (*mLogger) << t;
120         return (*this);
121     }
122  };
123
124 #define CAmLogger (*CAmLog::getDefaultLog())
125
126
127 #endif /* CAMLOG_H_ */