]>
cygwin.com Git - cygwin-apps/setup.git/blob - LogFile.cc
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
12 * Written by Robert Collins <rbtcollins@hotmail.com>
16 /* Log to one or more files. */
19 static const char *cvsid
=
24 #include "io_stream.h"
34 /* private helper class */
52 static LogEnt
*first_logent
= 0;
53 static LogEnt
**next_logent
= &first_logent
;
54 static LogEnt
*currEnt
= 0;
57 static list
<filedef
, String
, String::casecompare
> files
;
58 static ostrstream
*theStream
;
62 theStream
= new ostrstream
;
63 rdbuf (theStream
->rdbuf());
68 LogFile::clearFiles ()
70 while (files
.number())
72 filedef
*f
= files
.removebyindex(1);
78 LogFile::setFile (int minlevel
, String
const &path
, bool append
)
80 filedef
*f
= files
.getbykey (path
);
85 files
.registerbyobject (*f
);
92 LogFile::exit (int const exit_code
)
94 static int been_here
= 0;
104 note (NULL
, exit_msg
);
106 log (LOG_TIMESTAMP
) << "Ending cygwin install" << endLog
;
108 for (unsigned int i
= 1; i
<= files
.number(); ++i
)
110 filedef
*f
= files
[i
];
111 log_save (f
->level
, f
->key
, f
->append
);
114 ExitProcess (exit_code
);
121 LogFile::log_save (int babble
, String
const &filename
, bool append
)
123 static int been_here
= 0;
128 io_stream::mkpath_p (PATH_TO_FILE
, String("file://") + filename
);
130 io_stream
*f
= io_stream::open(String("file://") + filename
, append
? "at" : "wt");
133 fatal (NULL
, IDS_NOLOGFILE
, filename
.cstr_oneuse());
139 for (l
= first_logent
; l
; l
= l
->next
)
141 if (babble
|| !(l
->level
== LOG_BABBLE
))
143 char *tstr
= l
->msg
.cstr();
144 f
->write (tstr
, strlen (tstr
));
145 if (tstr
[strlen (tstr
) - 1] != '\n')
155 LogFile::operator() (log_level theLevel
)
157 if (theLevel
< 1 || theLevel
> 2)
160 theStream
= new ostrstream
;
161 rdbuf (theStream
->rdbuf());
162 currEnt
= new LogEnt
;
164 currEnt
->level
= theLevel
;
173 /* get a default LogEnt */
174 currEnt
= new LogEnt
;
176 currEnt
->level
= LOG_PLAIN
;
178 *next_logent
= currEnt
;
179 next_logent
= &(currEnt
->next
);
180 time (&(currEnt
->when
));
181 if (currEnt
->level
== LOG_TIMESTAMP
)
184 struct tm
*tm
= localtime (&(currEnt
->when
));
185 strftime (b
, 1000, "%Y/%m/%d %H:%M:%S ", tm
);
188 /* What follows is a hack to get around an (apparent) bug in libg++-3 with
189 * non-0 memory on alloc
191 currEnt
->msg
+= string(theStream
->str()).substr(0,theStream
->pcount()).c_str();
192 msg ("LOG: %d %s", currEnt
->level
, string(theStream
->str()).substr(0,theStream
->rdbuf()->pcount()).c_str());
193 theStream
->freeze(0);
195 /* reset for next use */
196 theStream
= new ostrstream
;
197 rdbuf (theStream
->rdbuf());
198 init (theStream
->rdbuf());
This page took 0.098477 seconds and 5 git commands to generate.