]> cygwin.com Git - cygwin-apps/setup.git/blob - LogFile.cc
2002-07-15 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / LogFile.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 /* Log to one or more files. */
17
18 #if 0
19 static const char *cvsid =
20 "\n%%% $Id$\n";
21 #endif
22
23 #include "LogFile.h"
24 #include "io_stream.h"
25 #include "win32.h"
26 #include "msg.h"
27 #include "resource.h"
28 #include <iostream>
29 #include <strstream>
30 #include <set>
31 #include <time.h>
32 #include <string>
33
34 /* private helper class */
35 class filedef
36 {
37 public:
38 int level;
39 String key;
40 bool append;
41 filedef (String const &_path) : key (_path) {}
42 bool operator == (filedef const &rhs) const
43 {
44 return key.casecompare (rhs.key) == 0;
45 }
46 bool operator < (filedef const &rhs) const
47 {
48 return key.casecompare (rhs.key) < 0;
49 }
50 };
51
52 /* another */
53 struct LogEnt
54 {
55 LogEnt *next;
56 enum log_level level;
57 time_t when;
58 String msg;
59 };
60
61 static LogEnt *first_logent = 0;
62 static LogEnt **next_logent = &first_logent;
63 static LogEnt *currEnt = 0;
64 int exit_msg = 0;
65
66 typedef set<filedef> FileSet;
67 static FileSet files;
68 static ostrstream *theStream;
69
70 LogFile::LogFile()
71 {
72 theStream = new ostrstream;
73 rdbuf (theStream->rdbuf());
74 }
75 LogFile::~LogFile(){}
76
77 void
78 LogFile::clearFiles ()
79 {
80 files.clear ();
81 }
82
83 void
84 LogFile::setFile (int minlevel, String const &path, bool append)
85 {
86 FileSet::iterator f = files.find (filedef(path));
87 if (f != files.end ())
88 files.erase (f);
89
90 filedef t (path);
91 t.level = minlevel;
92 t.append = append;
93 files.insert (t);
94 }
95
96 void
97 LogFile::exit (int const exit_code)
98 {
99 static int been_here = 0;
100 if (been_here)
101 #ifndef _CYGWIN_
102 ExitProcess (1);
103 #else
104 exit (1);
105 #endif
106 been_here = 1;
107
108 if (exit_msg)
109 note (NULL, exit_msg);
110
111 log (LOG_TIMESTAMP) << "Ending cygwin install" << endLog;
112
113 for (FileSet::iterator i = files.begin();
114 i != files.end(); ++i)
115 {
116 log_save (i->level, i->key, i->append);
117 }
118 #ifndef _CYGWIN_
119 ExitProcess (exit_code);
120 #else
121 exit (1);
122 #endif
123 }
124
125 void
126 LogFile::log_save (int babble, String const &filename, bool append)
127 {
128 static int been_here = 0;
129 if (been_here)
130 return;
131 been_here = 1;
132
133 io_stream::mkpath_p (PATH_TO_FILE, String("file://") + filename);
134
135 io_stream *f = io_stream::open(String("file://") + filename, append ? "at" : "wt");
136 if (!f)
137 {
138 fatal (NULL, IDS_NOLOGFILE, filename.cstr_oneuse());
139 return;
140 }
141
142 LogEnt *l;
143
144 for (l = first_logent; l; l = l->next)
145 {
146 if (babble || !(l->level == LOG_BABBLE))
147 {
148 char *tstr = l->msg.cstr();
149 f->write (tstr, strlen (tstr));
150 if (tstr[strlen (tstr) - 1] != '\n')
151 f->write ("\n", 1);
152 }
153 }
154
155 delete f;
156 been_here = 0;
157 }
158
159 ostream &
160 LogFile::operator() (log_level theLevel)
161 {
162 if (theLevel < 1 || theLevel > 2)
163 throw "barfoo";
164 if (!theStream)
165 theStream = new ostrstream;
166 rdbuf (theStream->rdbuf());
167 currEnt = new LogEnt;
168 currEnt->next = 0;
169 currEnt->level = theLevel;
170 return *this;
171 }
172
173 void
174 LogFile::endEntry()
175 {
176 if (!currEnt)
177 {
178 /* get a default LogEnt */
179 currEnt = new LogEnt;
180 currEnt->next = 0;
181 currEnt->level = LOG_PLAIN;
182 }
183 *next_logent = currEnt;
184 next_logent = &(currEnt->next);
185 time (&(currEnt->when));
186 if (currEnt->level == LOG_TIMESTAMP)
187 {
188 char b[100];
189 struct tm *tm = localtime (&(currEnt->when));
190 strftime (b, 1000, "%Y/%m/%d %H:%M:%S ", tm);
191 currEnt->msg = b;
192 }
193 /* What follows is a hack to get around an (apparent) bug in libg++-3 with
194 * non-0 memory on alloc
195 */
196 currEnt->msg += string(theStream->str()).substr(0,theStream->pcount()).c_str();
197 msg ("LOG: %d %s", currEnt->level, string(theStream->str()).substr(0,theStream->rdbuf()->pcount()).c_str());
198 theStream->freeze(0);
199 delete theStream;
200 /* reset for next use */
201 theStream = new ostrstream;
202 rdbuf (theStream->rdbuf());
203 init (theStream->rdbuf());
204 }
This page took 0.043381 seconds and 5 git commands to generate.