]>
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"
36 /* private helper class */
43 filedef (String
const &_path
) : key (_path
) {}
44 bool operator == (filedef
const &rhs
) const
46 return key
.casecompare (rhs
.key
) == 0;
48 bool operator < (filedef
const &rhs
) const
50 return key
.casecompare (rhs
.key
) < 0;
63 static LogEnt
*first_logent
= 0;
64 static LogEnt
**next_logent
= &first_logent
;
65 static LogEnt
*currEnt
= 0;
68 typedef set
<filedef
> FileSet
;
70 static stringbuf
*theStream
;
73 LogFile::createLogFile()
75 theStream
= new std::stringbuf
;
76 return new LogFile(theStream
);
79 LogFile::LogFile(std::stringbuf
*aStream
) : LogSingleton (aStream
)
85 LogFile::clearFiles ()
91 LogFile::setFile (int minlevel
, String
const &path
, bool append
)
93 FileSet::iterator f
= files
.find (filedef(path
));
94 if (f
!= files
.end ())
104 LogFile::exit (int const exit_code
)
106 static int been_here
= 0;
116 note (NULL
, exit_msg
);
118 log (LOG_TIMESTAMP
) << "Ending cygwin install" << endLog
;
120 for (FileSet::iterator i
= files
.begin();
121 i
!= files
.end(); ++i
)
123 log_save (i
->level
, i
->key
, i
->append
);
126 ExitProcess (exit_code
);
133 LogFile::log_save (int babble
, String
const &filename
, bool append
)
135 static int been_here
= 0;
140 io_stream::mkpath_p (PATH_TO_FILE
, String("file://") + filename
);
142 io_stream
*f
= io_stream::open(String("file://") + filename
, append
? "at" : "wt");
145 fatal (NULL
, IDS_NOLOGFILE
, filename
.cstr_oneuse());
151 for (l
= first_logent
; l
; l
= l
->next
)
153 if (babble
|| !(l
->level
== LOG_BABBLE
))
155 char *tstr
= l
->msg
.cstr();
156 f
->write (tstr
, strlen (tstr
));
157 if (tstr
[strlen (tstr
) - 1] != '\n')
167 LogFile::operator() (log_level theLevel
)
169 if (theLevel
< 1 || theLevel
> 2)
172 theStream
= new std::stringbuf
;
174 currEnt
= new LogEnt
;
176 currEnt
->level
= theLevel
;
185 /* get a default LogEnt */
186 currEnt
= new LogEnt
;
188 currEnt
->level
= LOG_PLAIN
;
190 *next_logent
= currEnt
;
191 next_logent
= &(currEnt
->next
);
192 time (&(currEnt
->when
));
193 if (currEnt
->level
== LOG_TIMESTAMP
)
196 struct tm
*tm
= localtime (&(currEnt
->when
));
197 strftime (b
, 1000, "%Y/%m/%d %H:%M:%S ", tm
);
200 /* What follows is a hack to get around an (apparent) bug in libg++-3 with
201 * non-0 memory on alloc
203 currEnt
->msg
+= theStream
->str();
204 // OLD code (libg++3) string(theStream->str()).substr(0,theStream->pcount()).c_str();
205 msg ("LOG: %d %s", currEnt
->level
, theStream
->str().c_str());
206 //string(theStream->str()).substr(0,theStream->rdbuf()->pcount()).c_str());
207 // theStream->freeze(0);
209 /* reset for next use */
210 theStream
= new std::stringbuf
;
This page took 0.043699 seconds and 5 git commands to generate.