ChessUSA.com TalkChess.com
Hosted by Your Move Chess & Games
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A logging facility
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions Flat
View previous topic :: View next topic  
Author Message
Marco Costalba



Joined: 14 Jun 2008
Posts: 2097

PostPost subject: Re: A logging facility    Posted: Tue Mar 20, 2012 8:30 pm Reply to topic Reply with quote

Ok, this is the final version, cleaned up a lot and with added direction indicators (">>" and "<<") so to easily understand from the log file which are the commands sent by GUI and which are the lines sent by engine. The Logger class is fully self contained, it doesn't need any external code, you can simply cut 'n paste the following whenever you want, as long as is a GPL code Smile

Code:

class Logger {

  Logger() : in(cin.rdbuf(), file), out(cout.rdbuf(), file) {}
  ~Logger() { start(false); }

  struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout

    Tie(streambuf* b, ofstream& f) : buf(b), file(f) {}

    int sync() { return file.rdbuf()->pubsync(), buf->pubsync(); }
    int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
    int underflow() { return buf->sgetc(); }
    int uflow() { return log(buf->sbumpc(), ">> "); }

    int log(int c, const char* prefix) {

      static int last = '\n';

      if (last == '\n')
          file.rdbuf()->sputn(prefix, 3);

      return last = file.rdbuf()->sputc((char)c);
    }

    streambuf* buf;
    ofstream& file;
  };

  ofstream file;
  Tie in, out;

public:
  static void start(bool b) {

    static Logger l;

    if (b && !l.file.is_open())
    {
        l.file.open("io_log.txt", ifstream::out | ifstream::app);
        cin.rdbuf(&l.in);
        cout.rdbuf(&l.out);
    }
    else if (!b && l.file.is_open())
    {
        cout.rdbuf(l.out.buf);
        cin.rdbuf(l.in.buf);
        l.file.close();
    }
  }
};


To start / stop logging of your engine you just need:

Code:

Logger::start(true); // To start logging

....

Logger::start(false); // To stop logging


As you can see Logger::start() is static method so you even don't need to instantiate the class. Tested on both MSVC and gcc.


P.S: This code is GPL, the original code is not but this version is completely different and substantially enhanced, totally written by me and added to Stockfish.
Back to top
View user's profile Send private message
Display posts from previous:   
Subject Author Date/Time
A logging facility Marco Costalba Sun Mar 18, 2012 10:23 pm
      Re: A logging facility Gary Sun Mar 18, 2012 11:56 pm
      Re: A logging facility Robert Hyatt Mon Mar 19, 2012 3:50 am
            Re: A logging facility Marco Costalba Mon Mar 19, 2012 7:18 am
                  Re: A logging facility Vincent Diepeveen Mon Mar 19, 2012 7:37 am
                        Re: A logging facility Marco Costalba Mon Mar 19, 2012 7:50 am
                              Re: A logging facility Vincent Diepeveen Mon Mar 19, 2012 8:22 am
                                    Re: A logging facility Jan Brouwer Mon Mar 19, 2012 6:43 pm
                                          Re: A logging facility Robert Hyatt Mon Mar 19, 2012 6:54 pm
                                                Re: A logging facility Jan Brouwer Mon Mar 19, 2012 8:00 pm
                                                      Re: A logging facility Marco Costalba Mon Mar 19, 2012 9:09 pm
                                                            Re: A logging facility Daniel Shawul Mon Mar 19, 2012 9:37 pm
                                                            Re: A logging facility Marco Costalba Tue Mar 20, 2012 8:30 pm
                                                Re: A logging facility Ben-Hur Carlos Langoni Wed Mar 21, 2012 2:16 am
                                                      Re: A logging facility Daniel Shawul Wed Mar 21, 2012 9:20 am
                                                            Re: A logging facility Ben-Hur Carlos Langoni Wed Mar 21, 2012 1:54 pm
                                                                  Re: A logging facility Daniel Shawul Wed Mar 21, 2012 3:38 pm
                                                            Re: A logging facility Marco Costalba Thu Mar 22, 2012 8:01 pm
                                                                  Re: A logging facility Daniel Shawul Thu Mar 22, 2012 10:48 pm
                                                                        Re: A logging facility Rein Halbersma Fri Mar 23, 2012 7:03 am
                                                                              Re: A logging facility Daniel Shawul Fri Mar 23, 2012 10:04 am
                                                                                    Re: A logging facility Rein Halbersma Fri Mar 23, 2012 12:32 pm
                                                                                          Re: A logging facility Martin Sedlak Fri Mar 23, 2012 1:00 pm
                                                                                          Re: A logging facility Daniel Shawul Fri Mar 23, 2012 2:05 pm
                                                      Re: A logging facility Robert Hyatt Wed Mar 21, 2012 4:36 pm
                                                      Re: A logging facility Mark Pearce Wed Mar 28, 2012 12:49 pm
                  Re: A logging facility Rein Halbersma Mon Mar 19, 2012 7:50 am
                        Re: A logging facility Marco Costalba Mon Mar 19, 2012 8:00 am
                              Re: A logging facility Rein Halbersma Mon Mar 19, 2012 8:09 am
                        Re: A logging facility Robert Hyatt Mon Mar 19, 2012 6:56 pm
      Re: A logging facility Daniel Shawul Mon Mar 19, 2012 7:52 pm
            Re: A logging facility Robert Hyatt Wed Mar 21, 2012 1:26 am
                  Re: A logging facility Daniel Shawul Wed Mar 21, 2012 9:00 am
      Re: A logging facility H.G.Muller Wed Mar 21, 2012 7:58 am
            Re: A logging facility Vincent Diepeveen Wed Mar 21, 2012 2:39 pm
                  Re: A logging facility H.G.Muller Wed Mar 21, 2012 5:29 pm
            Re: A logging facility H.G.Muller Thu Mar 22, 2012 8:11 am
                  Re: A logging facility Daniel Shawul Thu Mar 22, 2012 11:18 pm
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
Enhanced with Moby Threads