]> cygwin.com Git - cygwin-apps/setup.git/blob - LogSingleton.cc
excise "using namespace std;" and explicitly use a "std::" prefix throughout instead
[cygwin-apps/setup.git] / LogSingleton.cc
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 #include <stdarg.h>
19
20 /* Helper functions */
21
22 /* End of a Log comment */
23 std::ostream& endLog(std::ostream& outs)
24 {
25 /* Doesn't seem to be any way around this */
26 dynamic_cast<LogSingleton &>(outs).endEntry();
27 return outs;
28 }
29
30 /* The LogSingleton class */
31
32 LogSingleton * LogSingleton::theInstance(0);
33
34 LogSingleton::LogSingleton(std::streambuf* aStream) : std::ios (aStream), std::ostream (aStream)
35 {
36 std::ios::init (aStream);
37 }
38 LogSingleton::~LogSingleton(){}
39
40 LogSingleton &
41 LogSingleton::GetInstance()
42 {
43 if (!theInstance)
44 throw new std::invalid_argument ("No instance has been set!");
45 return *theInstance;
46 }
47
48 void
49 LogSingleton::SetInstance(LogSingleton &newInstance)
50 {
51 theInstance = &newInstance;
52 }
53
54 #if 0
55 // Logging class. Default logging level is PLAIN.
56 class LogSingleton : public std::ostream
57 {
58 public:
59 // Singleton support
60 /* Some platforms don't call destructors. So this call exists
61 * which guarantees to flush any log data...
62 * but doesn't call generic C++ destructors
63 */
64 virtual void exit (int const exit_code) __attribute__ ((noreturn));
65 // get a specific verbosity stream.
66 virtual ostream &operator() (enum log_level level);
67
68 friend ostream& endLog(ostream& outs);
69
70 protected:
71 LogSingleton (LogSingleton const &); // no copy constructor
72 LogSingleton &operator = (LogSingleton const&); // no assignment operator
73 void endEntry(); // the current in-progress entry is complete.
74 private:
75 static LogSingleton *theInstance;
76 };
77 #endif
78
79 // Log adapators for printf-style output
80 void
81 LogBabblePrintf(const char *fmt, ...)
82 {
83 int len;
84 char buf[2000];
85 va_list args;
86 va_start (args, fmt);
87 len = vsnprintf (buf, 2000, fmt, args);
88 if ((len > 0) && (buf[len-1] == '\n'))
89 buf[len-1] = 0;
90 Log (LOG_BABBLE) << buf << endLog;
91 }
92
93 void
94 LogPlainPrintf(const char *fmt, ...)
95 {
96 int len;
97 char buf[2000];
98 va_list args;
99 va_start (args, fmt);
100 len = vsnprintf (buf, 2000, fmt, args);
101 if ((len > 0) && (buf[len-1] == '\n'))
102 buf[len-1] = 0;
103 Log (LOG_PLAIN) << buf << endLog;
104 }
This page took 0.045295 seconds and 6 git commands to generate.