Drop setting root_scope as a side-effect of read_mounts()
[cygwin-setup.git] / LogSingleton.cc
blobb123351b3afdb6652a779fe1972e22cf90d69adc
1 /*
2 * Copyright (c) 2002, Robert Collins..
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.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by Robert Collins <rbtcollins@hotmail.com>
16 #include "LogSingleton.h"
17 #include <stdexcept>
18 #include <stdarg.h>
20 /* Helper functions */
22 /* End of a Log comment */
23 std::ostream& endLog(std::ostream& outs)
25 /* Doesn't seem to be any way around this */
26 dynamic_cast<LogSingleton &>(outs).endEntry();
27 return outs;
30 /* The LogSingleton class */
32 LogSingleton * LogSingleton::theInstance(0);
34 LogSingleton::LogSingleton(std::streambuf* aStream) : std::ios (aStream), std::ostream (aStream)
36 std::ios::init (aStream);
38 LogSingleton::~LogSingleton(){}
40 LogSingleton &
41 LogSingleton::GetInstance()
43 if (!theInstance)
44 throw new std::invalid_argument ("No instance has been set!");
45 return *theInstance;
48 void
49 LogSingleton::SetInstance(LogSingleton &newInstance)
51 theInstance = &newInstance;
54 // Log adapators for printf-style output
55 void
56 LogBabblePrintf(const char *fmt, ...)
58 int len;
59 char buf[8192];
60 va_list args;
61 va_start (args, fmt);
62 len = vsnprintf (buf, 8192, fmt, args);
63 if ((len > 0) && (buf[len-1] == '\n'))
64 buf[len-1] = 0;
65 Log (LOG_BABBLE) << buf << endLog;
68 void
69 LogPlainPrintf(const char *fmt, ...)
71 int len;
72 char buf[8192];
73 va_list args;
74 va_start (args, fmt);
75 len = vsnprintf (buf, 8192, fmt, args);
76 if ((len > 0) && (buf[len-1] == '\n'))
77 buf[len-1] = 0;
78 Log (LOG_PLAIN) << buf << endLog;