view anagram/support/log.h @ 17:12171da8943f

Don't refer to CVS.
author David A. Holland
date Tue, 31 May 2022 01:56:37 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
 * Copyright 2006 David A. Holland. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * log.h - logging
 */

#ifndef LOG_H
#define LOG_H

#ifdef VACLGUI
#include <ipoint.hpp>
#include <irect.hpp>
#include <istring.hpp>
#include <iexcbase.hpp>
#endif

class AgString; // from agstr.h
struct cint; // from cint.h

#ifdef INCLUDE_LOGGING

enum LogSwitch {
  LOG_OFF,		// logging is disabled
  LOG_MAYBE,		// logging switch inherits from context
  LOG_ON,		// logging is enabled
};

class LogSection {
  protected:
    LogSection *previous;
    const char *name;
    LogSwitch saved_switch;
  public:
    // the name passed in should be a string constant
    LogSection(const char *name, LogSwitch requested_setting);
    ~LogSection();

    static void logstack();
};

/* start a nested (sub)section, which lasts until end of scope */
#define LOGSECTION(name)     LogSection myLogSection(name, LOG_MAYBE)
#define LOGSECTION_OFF(name) LogSection myLogSection(name, LOG_OFF)
#define LOGSECTION_ON(name)  LogSection myLogSection(name, LOG_ON)

/* Logging macros. */

/*
 * LOGV logs a variable, with its name and value.
 * LOGS logs a literal string.
 */

#define LOGV(v) (logeol(), dolog(#v " = "), dolog(v))
#define LOGS(s) (logeol(), dolog(s))

/* The L... forms are for appending after a LOGV/LOGS in the same expression.*/
#define LV(v) , (dolog(" " #v " = "), dolog(v))
#define LS(s) , (dolog(" "), dolog(s))

/* The LC... forms are the same but insert a comma. */
#define LCV(v) , (dolog(", " #v " = "), dolog(v))
#define LCS(s) , (dolog(", "), dolog(s))

/* LOGX is for sticking in front of LV/LCV/whatnot to avoid the newline */
#define LOGX() ((void)0)

/* Misc. */
#define LOGINIT()
#define LOGSTACK        LogSection::logstack()
#define LOGEXCEPTION(e) logexception(e)

/* This can be used to enclose bits of expression only used when logging. */
#define IFLOG(x) x

/* The underlying functions */
void logeol(void);
void logtime(void);

void dolog(const char *);
void dolog(const AgString &);
void dolog(void *);
void dolog(int);
void dolog(cint);

#ifdef VACLGUI
void logexception(IException &);
void dolog(const IString &s);
void dolog(const IPair &p);
void dolog(const IRectangle &r);
#endif


#else

/* With logging off, nothing exists */

#define LOGSECTION(name)
#define LOGSECTION_OFF(name)
#define LOGSECTION_ON(name)
#define LOGV(x)
#define LOGS(x)
#define LV(x)
#define LS(x)
#define LCV(x)
#define LCS(X)
#define LOGX()
#define LOGINIT()
#define LOGSTACK
#define LOGEXCEPTION(e)

#define IFLOG(x)

#endif /* INCLUDE_LOGGING */

#endif /* LOG_H */