summaryrefslogtreecommitdiff
path: root/i/marvin/src/log/logger_file.cc
blob: 011a470e19f5874cce34b56ea773b9f5c1c062c1 (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
135
136
137
138
139
140
141
142
143
144
// logger_file.cc
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Dufour J�r�my
//
// Robot APB Team/Efrei 2004.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "logger_file.hh"

#include <stdexcept>
#include <fstream>
#include <iostream>

LoggerFile::t_files_ LoggerFile::files_;

/// Default constructor.
LoggerFile::LoggerFile (const std::string &param)
    : filename_ (param)
{
    if (param.empty ())
	throw std::invalid_argument ("Missing filename for file logger");
    file_ = getFile ();
}

/// Default destructor.
LoggerFile::~LoggerFile (void)
{
    try
      {
	releaseFile ();
      }
    catch (std::runtime_error &)
      {
      }
}

/// Get a ofstream associated to the filename.
/// This function manage multiple log to the same file by couting the number
/// reference to the file.
std::ofstream *
LoggerFile::getFile (void)
{
    t_files_::iterator i = files_.find (filename_);
    // If file does not exist
    if (i == files_.end ())
      {
	// Create it
	std::ofstream *file = new std::ofstream (filename_.c_str ());
	// Add it
	files_[filename_] = t_pair_files_ (1, file);
	return file;
      }
    else
      {
	// Increment number of reference
	t_pair_files_ &pair_file = i->second;
	pair_file.first++;
	return pair_file.second;
      }
}

/// Release the file from the map if needed. Return true if deleted.
bool
LoggerFile::releaseFile (void)
{
    t_files_::iterator i = files_.find (filename_);
    // If file does not exist
    if (i == files_.end ())
	throw std::runtime_error ("Trying to release an unknown file ?!");
    else
      {
	t_pair_files_ &pair_file = i->second;
	// Decrement number of reference
	pair_file.first--;
	// Delete it if needed
	if (pair_file.first == 0)
	  {
	    delete pair_file.second;
	    files_.erase (i);
	    return true;
	  }
      }
    return false;
}

/// Called at the begining of a message.
void
LoggerFile::start (const Log &log, const char *msg, Log::Level level)
{
    *file_ << log.getModule () << ':';
    const char *instance = log.getInstance ();
    if (instance)
        *file_ << ' ' << instance << ':';
    *file_ << " (" << msg << ')';
}

/// Output a string or a variable name.
LoggerFile &
LoggerFile::operator<< (const char *s)
{
    *file_ << ' ' << s;
    return *this;
}

/// Output a string or a variable name.
LoggerFile &
LoggerFile::operator<< (const std::string &s)
{
    *file_ << ' ' << s;
    return *this;
}

/// Output a integer.
LoggerFile &
LoggerFile::operator<< (int i)
{
    *file_ << ' ' << i;
    return *this;
}

/// Output a double.
LoggerFile &
LoggerFile::operator<< (double d)
{
    *file_ << ' ' << d;
    return *this;
}