e3b454c1cfcbb4bca01774b49a9a4a87fafe9869
[senf.git] / senf / Utils / Logger / FileTarget.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FileTarget public header */
25
26 #ifndef HH_SENF_Utils_Logger_FileTarget_
27 #define HH_SENF_Utils_Logger_FileTarget_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31 #include <boost/shared_ptr.hpp>
32 #include <fstream>
33 #include "IOStreamTarget.hh"
34
35 //#include "FileTarget.mpp"
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 namespace senf {
39
40     namespace console { class DirectoryNode; }
41
42 namespace log {
43
44     /** \brief Log target writing to a %log file.
45
46         The FileTarget will save all %log messages in the given file. Messages will be appended at
47         the end of the file.
48
49         \code
50         senf::log::FileTarget target ("file.name");
51
52         // Route all messages to this file.
53         target.route();
54         \endcode
55
56         After %log files have been rotated, the reopen() member should be called to create a new %log
57         file.
58
59         \ingroup targets
60       */
61     class FileTarget
62         : private boost::base_from_member<std::ofstream>,
63           public IOStreamTarget
64     {
65         typedef boost::base_from_member<std::ofstream> ofstream_t;
66
67     public:
68         //-////////////////////////////////////////////////////////////////////////
69         ///\name Structors and default members
70         //\{
71
72         explicit FileTarget(std::string const & filename, std::string const & nodename = "");
73                                         ///< Construct FileTarget writing to \a file
74
75         //\}
76         //-////////////////////////////////////////////////////////////////////////
77
78         void reopen();                  ///< Reopen %log after log-file rotation
79         void reopen(std::string const & file);  ///< Reopen %log under a new name
80
81         std::string const & filename() const; ///< Return current log file name
82
83     private:
84         std::string file_;
85
86         struct RegisterConsole {
87             RegisterConsole();
88             static boost::shared_ptr<senf::console::DirectoryNode> create(
89                 std::string const & filename, std::string const & nodename);
90             static RegisterConsole instance;
91         };
92     };
93
94 }}
95
96 //-/////////////////////////////////////////////////////////////////////////////////////////////////
97 //#include "FileTarget.cci"
98 //#include "FileTarget.ct"
99 //#include "FileTarget.cti"
100 #endif
101
102 \f
103 // Local Variables:
104 // mode: c++
105 // fill-column: 100
106 // comment-column: 40
107 // c-file-style: "senf"
108 // indent-tabs-mode: nil
109 // ispell-local-dictionary: "american"
110 // compile-command: "scons -u test"
111 // End: