]>
Commit | Line | Data |
---|---|---|
9f4a0c62 RC |
1 | /* |
2 | * Copyright (c) 2002, Robert Collins.. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | #include "LogSingleton.h" | |
17 | #include <stdexcept> | |
18 | ||
6625e635 RC |
19 | using namespace std; |
20 | ||
9f4a0c62 RC |
21 | /* Helper functions */ |
22 | ||
23 | /* End of a Log comment */ | |
24 | ostream& endLog(ostream& outs) | |
25 | { | |
26 | /* Doesn't seem to be any way around this */ | |
27 | dynamic_cast<LogSingleton &>(outs).endEntry(); | |
28 | return outs; | |
29 | } | |
30 | ||
31 | /* The LogSingleton class */ | |
32 | ||
33 | LogSingleton * LogSingleton::theInstance(0); | |
34 | ||
c83c52d8 RC |
35 | LogSingleton::LogSingleton(std::streambuf* aStream) : ios (aStream), ostream (aStream) |
36 | { | |
37 | ios::init (aStream); | |
38 | } | |
9f4a0c62 RC |
39 | LogSingleton::~LogSingleton(){} |
40 | ||
41 | LogSingleton & | |
42 | LogSingleton::GetInstance() | |
43 | { | |
44 | if (!theInstance) | |
45 | throw new invalid_argument ("No instance has been set!"); | |
46 | return *theInstance; | |
47 | } | |
48 | ||
49 | void | |
50 | LogSingleton::SetInstance(LogSingleton &newInstance) | |
51 | { | |
52 | theInstance = &newInstance; | |
53 | } | |
54 | ||
55 | #if 0 | |
56 | // Logging class. Default logging level is PLAIN. | |
57 | class LogSingleton : public ostream | |
58 | { | |
59 | public: | |
60 | // Singleton support | |
61 | /* Some platforms don't call destructors. So this call exists | |
62 | * which guarantees to flush any log data... | |
63 | * but doesn't call generic C++ destructors | |
64 | */ | |
65 | virtual void exit (int const exit_code) __attribute__ ((noreturn)); | |
66 | // get a specific verbosity stream. | |
67 | virtual ostream &operator() (enum log_level level); | |
68 | ||
69 | friend ostream& endLog(ostream& outs); | |
70 | ||
71 | protected: | |
72 | LogSingleton (LogSingleton const &); // no copy constructor | |
73 | LogSingleton &operator = (LogSingleton const&); // no assignment operator | |
74 | void endEntry(); // the current in-progress entry is complete. | |
75 | private: | |
76 | static LogSingleton *theInstance; | |
77 | }; | |
78 | #endif |