diff options
| author | Steven Fuller <relnev@icculus.org> | 2001-07-01 00:55:22 +0000 |
|---|---|---|
| committer | Patryk Obara <dreamer.tan@gmail.com> | 2019-08-20 02:09:04 +0200 |
| commit | 2186d5f3f95cd74a070a490d899291648d58667a (patch) | |
| tree | 55241a1afa3e1a22e0b6593a8dead0b703800f44 /src/win95/debuglog.hpp | |
| parent | 218ca90543758a20ac326e444ca0643174ca7384 (diff) | |
Initial revision
Diffstat (limited to 'src/win95/debuglog.hpp')
| -rw-r--r-- | src/win95/debuglog.hpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/win95/debuglog.hpp b/src/win95/debuglog.hpp new file mode 100644 index 0000000..5a0087e --- /dev/null +++ b/src/win95/debuglog.hpp @@ -0,0 +1,179 @@ +#ifndef _included_debuglog_hpp_ +#define _included_debuglog_hpp_ + +#include <stdio.h> +#include "debuglog.h" +#include "list_tem.hpp" + +/* Changed 27/1/98 by DHM: + ----------------------- + + Made LogFile derived from R_DumpContext rather than being a base class. + + This is in order to give a clean interface for debug dumps to e.g. the + screen, with the same interface as to a log file. This base class will + perform an analagous role to the class CDumpContext in the Microsoft + Foundation Class library. + + The virtual functions dputs(), dprintf() and vdprintf() will eventually replace + lputs(), lprintf() and vlprintf(). + + For the moment I've copied and pasted the implementations of both. I would prefer + in the short term to make one call the other, but this isn't easy with variable + arguments. In the long term I want to eliminate the l* functions (lputs() etc) + but can't because of heritage code (and heritage libraries). +*/ + + #ifndef _dcontext_hpp + #include "dcontext.hpp" + #endif + +struct LogFile : public R_DumpContext +{ +private: + char * fname; + List<char *> unwritten; + int ever_written; + void FlushOut(FILE * fp); + +public: + LogFile(char const * const _fname); + virtual ~LogFile(); + LogFile & operator = (LogFile const & l); + LogFile(LogFile const & l); + + // {{{ Virtual dump implementations: + inline int dputs(char const * const buf) + { + if (!fname) return EOF; + FILE * fp = fopen(fname,"a"); + if (!fp) + { + if (!ever_written) return EOF; + char * newtxt = new char [strlen(buf)+1]; + strcpy(newtxt,buf); + unwritten.add_entry_end(newtxt); + return 0; + } + if (unwritten.size()) FlushOut(fp); + ever_written = 1; + int rv = fputs(buf,fp); + fclose(fp); + return rv; + } + + inline int dprintf(char const * format, ... ) + { + if (!fname) return -1; + FILE * fp = fopen(fname,"a"); + if (!fp && !ever_written) return -1; + va_list ap; + va_start(ap, format); + int rv; + if (fp) + { + if (unwritten.size()) FlushOut(fp); + rv = vfprintf(fp,format,ap); + ever_written = 1; + } + else + { + char buf[4096]; + rv = vsprintf(buf,format,ap); + char * newtxt = new char [strlen(buf)+1]; + strcpy(newtxt,buf); + unwritten.add_entry_end(newtxt); + } + va_end(ap); + if (fp) fclose(fp); + return rv; + } + + inline int vdprintf(char const * format, va_list ap) + { + if (!fname) return -1; + FILE * fp = fopen(fname,"a"); + if (!fp && !ever_written) return -1; + + int rv; + if (fp) + { + if (unwritten.size()) FlushOut(fp); + rv = vfprintf(fp,format,ap); + ever_written = 1; + fclose(fp); + } + else + { + char buf[4096]; + rv = vsprintf(buf,format,ap); + char * newtxt = new char [strlen(buf)+1]; + strcpy(newtxt,buf); + unwritten.add_entry_end(newtxt); + } + return rv; + } + // }}} + + // {{{ Deprecated logging functions: + inline int lputs(char const * const buf) + { + return dputs(buf); + } + + inline int lprintf(char const * format, ... ) + { + if (!fname) return -1; + FILE * fp = fopen(fname,"a"); + if (!fp && !ever_written) return -1; + va_list ap; + va_start(ap, format); + int rv; + if (fp) + { + if (unwritten.size()) FlushOut(fp); + rv = vfprintf(fp,format,ap); + ever_written = 1; + } + else + { + char buf[4096]; + rv = vsprintf(buf,format,ap); + char * newtxt = new char [strlen(buf)+1]; + strcpy(newtxt,buf); + unwritten.add_entry_end(newtxt); + } + va_end(ap); + if (fp) fclose(fp); + return rv; + } + + inline int vlprintf(char const * format, va_list ap) + { + if (!fname) return -1; + FILE * fp = fopen(fname,"a"); + if (!fp && !ever_written) return -1; + + int rv; + if (fp) + { + if (unwritten.size()) FlushOut(fp); + rv = vfprintf(fp,format,ap); + ever_written = 1; + fclose(fp); + } + else + { + char buf[4096]; + rv = vsprintf(buf,format,ap); + char * newtxt = new char [strlen(buf)+1]; + strcpy(newtxt,buf); + unwritten.add_entry_end(newtxt); + } + return rv; + } + // }}} + +}; + +#endif // ! _included_debuglog_hpp_ |
