//=-- AccessLogManager.h - Keeps track of all of the AccessLog variants ------=
//
//  The class organizes all of the AccessLog's.  When the init method is
//  called, the AccessLogManager selects one AccessLog from the Plugin
//  object that matches the plugin specified in the configuration file.
//
//=---------------------------------------------------------------------------=
//  This file is copyright (c) 1997-1998 Chris Lattner
//=---------------------------------------------------------------------------=

#ifndef ACCESSLOGMANAGER_H
#define ACCESSLOGMANAGER_H

#include "LinkedList.h"
#include "Table.h"
#include "AccessLog.h"

class AccessFormatPlugin;
class AccessFilterPlugin;
class String;
class Serialize;
class VTExp;

class MSAPI AccessLogManager {
public :

  // Constructor - Trivially initialize data.
  //
  AccessLogManager();

  // Destructor - Free AccessLogPlugin, all of the AccessFilterPlugin's, and
  // clear out the AccessFilters list.
  ~AccessLogManager();
  
  // init - Select an AccessLogPlugin to load and load all of the needed
  // AccessFilterPlugin's.  
  //
  int init();

  // ReadAccess - Load an access from the plugin and then apply all of the
  // required AccessFilterPlugins to the access.  The returned pointer is valid
  // until the next call into the AccessLogManager.
  //
  AccessFormatPlugin *ReadAccess();

  // GetNumAccesses - Asks the plugin how many accesses it has processed so far
  //
  inline int GetNumAccesses() { return NumAccesses; }

  // LoadState - Load current state from serialization record...
  void LoadState(Serialize &I);

  // SaveState - Save current state out to a serialization record...
  void SaveState(Serialize &O);

private :
  static int MaxHitsPerDay;
  static int NumHitsReadToday;   // Used to implement -N command line option.

  // Add all of the AccessFilters to our access filter list...
  void ScanConfigFileForAccessFilters();

  // AddFilter - Add a filter to the internal list.  The Expr 'Command' 
  // should be the exp read from the [Global] section of the configuration 
  // file.
  //
  int AddFilter(const VTExp *Command);


  // AccessLogRecord - An agregate type that contains the AccessLog (P) for the
  // log file as well as the most recently loaded access from it (S).
  //
  typedef DataPair<AccessLog*, AccessFormatPlugin*> AccessLogRecord;

  // AccessLog that we are attached to, and the current log entries from
  // each log... the key of the hash table is the filename of the log...
  //
  Table<String, AccessLogRecord> AccessLogs;

  // NumAccessLogs - The number of entries in the AccessLogs table...
  int NumAccessLogs;

  AccessLogRecord *LastLogRead;

  // Number of accesses processed... returned by GetNumAccesses.
  int NumAccesses;

  // Have we read the first access from each file yet?
  int ReadFirstAccesses;

  // Ordered list of filters to apply.
  LinkedList<AccessFilterPlugin*> AccessFilters;
};

#endif
