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