summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/include/CAmLog.h
blob: 2cbf68a4ee2147a39cabffab6c27acef5d20c2cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * CAmLog.h
 *
 *  Created on: Jul 2, 2013
 *      Author: genius
 */

/**
 * Copyright (C) 2012, BMW AG
 *
 * This file is part of GENIVI Project AudioManager.
 *
 * Contributions are licensed to the GENIVI Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *
* \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
 *
 * \file CAmLog.h
 * For further information see http://www.genivi.org/.
 *
 */

#ifndef CAMLOG_H_
#define CAMLOG_H_

#include <iostream>
#include <iosfwd>
#include <stdio.h>
#include <stdexcept>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <assert.h>

/**
 * Implements a basic logging mechanism that can be used to print debug information into a file or to the console.
 * It can be used either as singleton through the appropriate method getDefaultLog() or as independent instantiated object.
 * The default initializer sets the console as output for newly created objects.
 * Example: CAmLogger << "Text"; //to print out through the singleton object directly to the console
 */

#define DEFAULT_LOG_FOLDER 		"/tmp/"
#define DEFAULT_LOGFILE_PREFIX "am_dump_"
#define DEFAULT_LOGFILE_EXT 	".log"

#define DEL( aPointer ) delete aPointer, aPointer = NULL;

/* */
typedef enum { eCAmLogNone = 0, eCAmLogStdout = 1, eCAmLogFile = 2 } eCAmLogType;

class CAmLog
{
private:
	/**
	 * Private classes which usually own (wrap) a stream object. They are responsible for creating and deleting it.
	 */
	class CAmLogger
	{
	protected:
		std::ostream* mOutputStream;
	public:
		CAmLogger ():mOutputStream(NULL) {};
		virtual ~CAmLogger () { };
		virtual void log(const std::string& _s)
	    {
	        (*mOutputStream) << _s;
	        mOutputStream->flush();
	    }
	    template <class T>
	    CAmLogger & operator << (const T & t)
	    {
	    	(*mOutputStream) << t;
	    	return (*this);
	    }
	};

	class CAmFileLogger : public CAmLogger
	{
		std::string mFilename;
	public:
		static void generateLogFilename(std::string &result);
		explicit CAmFileLogger(const std::string& _s) : CAmLogger()
		{
			mFilename = _s;
			mOutputStream = new std::ofstream(mFilename.c_str());
		}
		~CAmFileLogger();
	};

	class CAmStdOutLogger : public CAmLogger
	{
	public:
		CAmStdOutLogger()
		{
			mOutputStream = &std::cout;
		}
	};

private:
	eCAmLogType mLogType;
    CAmLogger* mLogger;

protected:
    void releaseLogger();
    void instantiateLogger( const eCAmLogType type);
public:
    CAmLog(const eCAmLogType type );
    CAmLog();
    ~CAmLog();

    static CAmLog *getDefaultLog();

    void setLogType( const eCAmLogType type);
    eCAmLogType getLogType() const;

    template <class T>
    CAmLog & operator << (const T & t)
    {
    	assert(mLogger!=NULL);
    	(*mLogger) << t;
    	return (*this);
    }
 };

#define CAmLogger (*CAmLog::getDefaultLog())


#endif /* CAMLOG_H_ */