30 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
37 using namespace basis;
56 static chaos __hidden_chaos;
57 return __hidden_chaos.
inclusive(0, 1280004);
60 file_logger::file_logger()
62 _file_limit(DEFAULT_LOG_FILE_SIZE),
75 name(initial_filename);
89 filename prog = application_configuration::application_name();
90 return application_configuration::make_logfile_name(prog.
rootname() +
".log");
100 void file_logger::close_file()
103 if (_outfile) _outfile->
flush();
112 *_filename = new_name;
115 int file_logger::size_reduction()
const
124 if (!_outfile && !_file_limit)
return true;
125 if (!_outfile)
return false;
126 return _outfile->
good();
138 if (!_outfile) open_file();
139 if (_outfile) _outfile->
flush();
142 bool file_logger::open_file()
162 for (
int i = 0; i < _filename->
length(); i++)
163 if ((*_filename)[i] ==
'\\') (*_filename)[i] =
'/';
167 filename temp_dir(temp_file.dirname());
168 if (!temp_dir.good() || !temp_dir.is_directory()) {
169 directory::recursive_create(temp_dir);
174 return _outfile->
good();
179 if (!_file_limit)
return common::OKAY;
181 size_t current_size = 0;
184 if (!
member(filter))
return common::OKAY;
185 if (!_outfile) open_file();
186 if (!_outfile)
return common::BAD_INPUT;
187 if (!_outfile->
good())
return common::BAD_INPUT;
196 astring end = parser_bits::platform_eol_to_chars();
199 current_size = _outfile->
tell();
204 if (current_size > _file_limit)
truncate(_file_limit - size_reduction());
210 if (!_file_limit)
return common::OKAY;
212 size_t current_size = 0;
215 if (!
member(filter))
return common::OKAY;
216 if (!_outfile) open_file();
217 if (!_outfile)
return common::BAD_INPUT;
218 if (!_outfile->
good())
return common::BAD_INPUT;
224 current_size = _outfile->
tell();
229 if (current_size > _file_limit)
230 truncate(_file_limit - size_reduction());
236 if (!_file_limit)
return common::OKAY;
240 if (!
member(filter))
return common::OKAY;
241 if (!_outfile) open_file();
242 if (!_outfile)
return common::BAD_INPUT;
243 if (!_outfile->
good())
return common::BAD_INPUT;
250 byte_formatter::text_dump(dumped_form, to_log);
268 if (!_outfile) open_file();
269 if (!_outfile)
return;
270 if (!_outfile->
good())
return;
273 size_t current_size = 0;
296 _outfile->
seek(0, byte_filer::FROM_END);
297 current_size = _outfile->
tell();
298 if (current_size <= new_size) {
304 astring new_file(astring::SPRINTF,
"%s.tmp.%d",
name().s(),
311 current_size = _outfile->
tell();
320 int start_of_keep = int(current_size - new_size);
323 _outfile->
seek(start_of_keep, byte_filer::FROM_START);
332 size_t bytes_written = 0;
336 while (!_outfile->
eof() && (bytes_written <= new_size)) {
342 bytes_written += bytes_read;
344 if (!_outfile->
eof() || bytes_read)
351 size_t hold_size = hold_stream->
tell();
356 hold_stream->
seek(0, byte_filer::FROM_START);
357 while (!hold_stream->
eof() && (bytes_written <= hold_size) ) {
363 bytes_written += bytes_read;
364 if (!hold_stream->
eof() || bytes_read)
const contents * observe() const
Returns a pointer to the underlying C array of data.
int length() const
Returns the current reported length of the allocated C array.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
int length() const
Returns the current length of the string.
auto_synchronizer simplifies concurrent code by automatically unlocking.
Defines the base class for all string processing objects in hoople.
virtual const char * observe() const =0
observes the underlying pointer to the zero-terminated string.
virtual int length() const =0
Returns the current length of the string.
A very common template for a dynamic array of bytes.
Outcomes describe the state of completion for an operation.
Provides file managment services using the standard I/O support.
int write(const basis::abyte *buffer, int buffer_size)
writes "buffer_size" bytes into the file from "buffer".
bool seek(int where, origins origin=FROM_START)
places the cursor in the file at "where", based on the "origin".
int read(basis::abyte *buffer, int buffer_size)
reads "buffer_size" bytes from the file into "buffer".
bool eof()
returns true if the cursor is at (or after) the end of the file.
size_t tell()
returns the current position within the file, in terms of bytes.
void flush()
forces any pending writes to actually be saved to the file.
bool good()
returns true if the file seems to be in the appropriate desired state.
Provides operations commonly needed on file names.
basis::astring rootname() const
returns the root part of the basename without an extension.
bool reopen()
closes the current file and attempts to reopen it.
bool good() const
returns true if the logger appears correctly hooked up to a file.
basis::astring name() const
observes the filename where logged information is written.
void flush()
causes any pending writes to be sent to the output file.
static basis::astring log_file_for_app_name()
returns a log file name for file_logger based on the program name.
void truncate(size_t new_size)
chops the file to ensure it doesn't go much over the file size limit.
file_logger()
creates a logger without a log file and with the default size limit.
basis::outcome log(const basis::base_string &info, int filter=basis::ALWAYS_PRINT)
writes information to the log file (if the filename is valid).
basis::outcome format_bytes(const basis::byte_array &to_log, int filter=basis::ALWAYS_PRINT)
fancifully formats a stream of bytes "to_log" and sends them into log.
basis::outcome log_bytes(const basis::byte_array &to_log, int filter=basis::ALWAYS_PRINT)
sends a stream of bytes "to_log" without interpretation into the log.
virtual bool member(int filter_to_check)
Returns true if the "filter_to_check" is a member of the filter set.
a platform-independent way to acquire random numbers in a specific range.
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
#define NULL_POINTER
The value representing a pointer to nothing.
The guards collection helps in testing preconditions and reporting errors.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
unsigned char abyte
A fairly important unit which is seldom defined...
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
const int MAXIMUM_BUFFER_SIZE
An extension to floating point primitives providing approximate equality.
A dynamic container class that holds any kind of object via pointers.