deriving stream classes
gotta question about simple stream class derivations. i don''t claim to know a whole bunch about the iostream classes, but it seems that there is lot of controversy about their usefulness (or, maybe its just me...)
first, let me explain my goal... i''m writing a new MUD codebase (for those of you who don''t know, a MUD is simply a telnet "game" - much like the old zorks. its completely text-based, and runs the server side, and acts like a special telnet server - at least, from my understanding). anyway, my target will eventually be on a Unix/Linux machine, though i am coding in win32.
secondly, the old codebases are written completely in ANSI C. i understand the advantage in that - since its more standard - and therefore, easier to port. i WOULD, however, like to write it in C++ ultimately as a learning experience. my general questions lie along the lines of:
is the standard for ANSI C++ (if it even exists) as solid as ANSI C? are they one and the same? gcc in linux is capable of handling C++ code -- but how problematic will the port from MSVC be to gcc?
anyway...on the my stream question:
i''ve come along a place in development where i''d like to add a solid logging system. i DO understand the power in C code such as sprintf and etc, but again, i''m doing a lot of this to "learn"
therefore, i''ve set out to write a derivation of the ofstream class to handle all my logging. ultimately, i''d like for this class to be able to handle:
(1) general logging -- logs everything i would normally use perror to handle, as well as general game "bugs" and/or special commands that should always be logged (ie: shutdown). this will all be in 1 (maybe 2) file, written in append mode...
(2) individual logging -- for me to be able to target a single user (or "character") in the mud and log ALL his actions. this will be kept in separate files, based on char''s name.
here''s my general thoughts on how i should attempt to do this.
there should be global instance of the class "logstream" (for instance glog) that will handle the former in my list. furthermore, each time a character is to be logged, i will further create another instance (upon his login) that will log his actions. this second log shall be a member of the character_data class. i would like to avoid as much overhead as possible (ie, i don''t want to create this instance, log 1 command, and destroy the instance). question #1: is it feasable to place a pointer of type logstream in his char_data -- to be initialized only if he''s to be logged, otherwise left NULL? #2: does this effectively save both memory and overhead on characters that are NOT to be logged? (log status is determined upon login, and thereafter checked with a simple:
if ( !ch->log )
{
(*(ch->plog)) << ch->last_command << endl;
// i''m not too sure about the pointers here. plog is a pointer of type logstream (ie, logstream *plog) -- so does that mean i must dereference it before i can call the operator<< ?
}
if this idea is sound, my next line of questioning involves the actual creation of such a class logstream. since i want to direct output not only to a file, but also to stdout (and perhaps even to an internal "channel" so that i can observe from within the mud) ...it seems that i MUST rewrite the operator<< function. to keep all the other abilities of iostream, i must derive my class from something (i have no clue what).
intuitively, i think that i should derive my class logstream from ofstream (for file i/o), and from within my operator<< function, call the other stream classes (cout, and whatever stream is created for internal communication inside the mud).
my function would look vaguely like this:
logstream& operator<< ( ... )
{
(*this)<<data; // sends output to either global log file, or individual log file
cout<<data; // sends to stdout
chanout<<data; // sends to internal channel
}
again, this is w/ the assumption that logstream itself is inherited from ofstream, and inertly knows how to open a file (done during construction/char login), and write to it, then close the file (during destruction/char logout)
i have NO idea how to create an operator function that will handle all (simple) data-types... nor do i know whether this code will be inefficient...
a simple outline about how to derive a streaming class that will allow me to write to multiple destinations (essentially stdout, a file, and probably a socket -- for local screen output, character/global logging, and internal channels, respectively) and a best guess opinion on the practicality of this idea will be very very much appreciated!
again, sorry for the long post...thanks again!
---
~khal
---~khal
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement