]>
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 */
41 filedef (String
const &_path
) : key (_path
) {}
42 bool operator == (filedef
const &rhs
) const
44 return key
.casecompare (rhs
.key
) == 0;
46 bool operator < (filedef
const &rhs
) const
48 return key
.casecompare (rhs
.key
) < 0;
61 static LogEnt
*first_logent
= 0;
62 static LogEnt
**next_logent
= &first_logent
;
63 static LogEnt
*currEnt
= 0;
66 typedef set
<filedef
> FileSet
;
68 static ostrstream
*theStream
;
72 theStream
= new ostrstream
;
73 rdbuf (theStream
->rdbuf());
78 LogFile::clearFiles ()
84 LogFile::setFile (int minlevel
, String
const &path
, bool append
)
86 FileSet::iterator f
= files
.find (filedef(path
));
87 if (f
!= files
.end ())
97 LogFile::exit (int const exit_code
)
99 static int been_here
= 0;
109 note (NULL
, exit_msg
);
111 log (LOG_TIMESTAMP
) << "Ending cygwin install" << endLog
;
113 for (FileSet::iterator i
= files
.begin();
114 i
!= files
.end(); ++i
)
116 log_save (i
->level
, i
->key
, i
->append
);
119 ExitProcess (exit_code
);
126 LogFile::log_save (int babble
, String
const &filename
, bool append
)
128 static int been_here
= 0;
133 io_stream::mkpath_p (PATH_TO_FILE
, String("file://") + filename
);
135 io_stream
*f
= io_stream::open(String("file://") + filename
, append
? "at" : "wt");
138 fatal (NULL
, IDS_NOLOGFILE
, filename
.cstr_oneuse());
144 for (l
= first_logent
; l
; l
= l
->next
)
146 if (babble
|| !(l
->level
== LOG_BABBLE
))
148 char *tstr
= l
->msg
.cstr();
149 f
->write (tstr
, strlen (tstr
));
150 if (tstr
[strlen (tstr
) - 1] != '\n')
160 LogFile::operator() (log_level theLevel
)
162 if (theLevel
< 1 || theLevel
> 2)
165 theStream
= new ostrstream
;
166 rdbuf (theStream
->rdbuf());
167 currEnt
= new LogEnt
;
169 currEnt
->level
= theLevel
;
178 /* get a default LogEnt */
179 currEnt
= new LogEnt
;
181 currEnt
->level
= LOG_PLAIN
;
183 *next_logent
= currEnt
;
184 next_logent
= &(currEnt
->next
);
185 time (&(currEnt
->when
));
186 if (currEnt
->level
== LOG_TIMESTAMP
)
189 struct tm
*tm
= localtime (&(currEnt
->when
));
190 strftime (b
, 1000, "%Y/%m/%d %H:%M:%S ", tm
);
193 /* What follows is a hack to get around an (apparent) bug in libg++-3 with
194 * non-0 memory on alloc
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);
200 /* reset for next use */
201 theStream
= new ostrstream
;
202 rdbuf (theStream
->rdbuf());
203 init (theStream
->rdbuf());
This page took 0.043381 seconds and 5 git commands to generate.