#ifndef _DMLOG_H
#define _DMLOG_H

#include <stdio.h>
#include <Vk/VkComponent.h>
#include <Vk/VkWindow.h>
#include <Vk/VkOptionMenu.h>
#include "IncrementalLine.h"


/*
CLASS 
DmLogWindow

DESCRIPTION
Displays contents of DataManager log file, highlights error messages

AUTHOR
Tom O'Reilly
*/
class DmLogWindow : public VkWindow {

public:

  DmLogWindow(const char *name,
	      const char *peerName,
	      const char *logFile);

  ~DmLogWindow();

  virtual const char *className() {
    return "DmLogWindow";
  }

  virtual void handleWmDeleteMessage()
  {
    hide();
  }

  ///////////////////////////////////////////////////////////////////
  // Return True if specified log file exists
  Boolean logFileFound() {
    return _logFileFound;
  }

  ///////////////////////////////////////////////////////////////////
  // Current number of info messages in log file
  int nInfoMsgs() {
    return _nInfoMsgs;
  }

  ///////////////////////////////////////////////////////////////////
  // Current number of warning messages in log file
  int nWarningMsgs() {
    return _nWarningMsgs;
  }

  ///////////////////////////////////////////////////////////////////
  // Current number of critical messages in log file
  int nCriticalMsgs() {
    return _nCriticalMsgs;
  }


protected:

  enum {
    LastBootOption = 0,
    EntireFileOption
  };

  ///////////////////////////////////////////////////////////////////
  // Opens log file for monitoring
  int init();

  Boolean _logFileFound;

  Widget _filterButtons;
  Widget _messageList;
  Widget _statusMsg;
  Widget _logFileNameWidget;

  VkOptionMenu *_optionMenu;

  char *_logFileName;
  int _logFileFd;
  XtInputId _inputId;
  int _tailLines;
  int _nListChars;
  pid_t _childPid;

  int _nInfoMsgs;
  int _nWarningMsgs;
  int _nCriticalMsgs;

  ///////////////////////////////////////////////////////////////////
  // Keep copy of last full line read from input; this line gets
  // parsed for error, warning, and info tags
  IncrementalLine _lastLine;

  ///////////////////////////////////////////////////////////////////
  // Read data from log file pipe and process
  void processNewData();

  ///////////////////////////////////////////////////////////////////
  // Character position of last boot in log file
  int findLastBoot();

  ///////////////////////////////////////////////////////////////////
  // Return True if message should be highlighted, else False
  Boolean highLightMsg(const char *msg);

  ///////////////////////////////////////////////////////////////////
  // Count and highlight critical/warning messages
  Boolean countMsgs();

  ///////////////////////////////////////////////////////////////////
  // Called when new log file name is entered
  static void logFileNameCallback(Widget w,
				  XtPointer clientData,
				  XtPointer callData);

  ///////////////////////////////////////////////////////////////////
  // Called when "browse" button is pressed
  static void browseCallback(Widget w,
			     XtPointer clientData,
			     XtPointer callData);

  ///////////////////////////////////////////////////////////////////
  // Called when item in option menu is selected
  static void optionCallback(Widget w,
			     XtPointer clientData,
			     XtPointer callData);

  ///////////////////////////////////////////////////////////////////
  // Called when data is ready in log file pipe
  static void dataReadyCallback(XtPointer clientData, int *fid, 
				XtInputId *id);

  ///////////////////////////////////////////////////////////////////
  // Highlight messages
  static Boolean workProc(XtPointer clientData);

};


#endif


