/*****************************************************************************
 * Copyright (c) 2002-2020 MBARI
 * Monterey Bay Aquarium Research Institute, all rights reserved.
 *****************************************************************************
 * @file    macrologger_main.h
 * @authors r. henthorn
 * @date    07/30/2020
 * @brief   Accompanying header file to macrologger.h to be included only once
 *          in an application.
 *
 * Project: LRAUV Obstacle avoidance
 * Summary: Application main() includes this file to declare the MLOUTSTREAM
 *          global variable and optionally assign the variable using the
 *          openMLOutstream() function to also log to a file.
 *****************************************************************************/
/*****************************************************************************
 * Copyright Information:
 * Copyright 2002-2020 MBARI
 * Monterey Bay Aquarium Research Institute, all rights reserved.
 *
 * Terms of Use:
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version. You can access the GPLv3 license at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details (http://www.gnu.org/licenses/gpl-3.0.html).
 *
 * MBARI provides the documentation and software code "as is", with no
 * warranty, express or implied, as to the software, title, non-infringement
 * of third party rights, merchantability, or fitness for any particular
 * purpose, the accuracy of the code, or the performance or results which you
 * may obtain from its use. You assume the entire risk associated with use of
 * the code, and you agree to be responsible for the entire cost of repair or
 * servicing of the program with which you are using the code.
 *
 * In no event shall MBARI be liable for any damages,whether general, special,
 * incidental or consequential damages, arising out of your use of the
 * software, including, but not limited to,the loss or corruption of your data
 * or damages of any kind resulting from use of the software, any prohibited
 * use, or your inability to use the software. You agree to defend, indemnify
 * and hold harmless MBARI and its officers, directors, and employees against
 * any claim,loss,liability or expense,including attorneys' fees,resulting from
 * loss of or damage to property or the injury to or death of any person
 * arising out of the use of the software.
 *
 * The MBARI software is provided without obligation on the part of the
 * Monterey Bay Aquarium Research Institute to assist in its use, correction,
 * modification, or enhancement.
 *
 * MBARI assumes no responsibility or liability for any third party and/or
 * commercial software required for the database or applications. Licensee
 * agrees to obtain and maintain valid licenses for any additional third party
 * software required.
 *****************************************************************************/

/******************************
 * Headers
 ******************************/
#ifndef __MACROLOGGER_MAIN_H__
#define __MACROLOGGER_MAIN_H__

#include <stdio.h>
#include "macrologger.h"

/* Global variable to add logging output to a file. Set to go to
 * only stderr by default. Application main() can set assign to an
 * opened FILE stream.
 */

FILE* MLOUTSTREAM = NULL;

char MLLOGLEVEL = INFO_LEVEL;

char MLVERBOSE = 1;

/******************************
 * Code
 ******************************/

/*
 * FILE *openMLOutstream(const char *logfilename)
 *
 *   Use the pathname in 'logfilename' variable to open a FILE stream in
 *   mode 'a' for appending, and assign the stream to MLOUTSTREAM. Closes
 *   the current MLOUTSTREAM if already opened.
 *
 *   Returns NULL if 'logfilename' is NULL.
 *   Otherwise, returns the result of fopen(logfilename, "a").
 */

static inline FILE *openMLOutstream(const char *logfilename)
{
   if (MLOUTSTREAM)
   {
      LOG_INFO("macrologger_main.h:openMLOutstream(): Closing current stream");
      fclose(MLOUTSTREAM);
   }

   if (NULL == logfilename)
   {
      LOG_ERROR("macrologger_main.h:openMLOutstream(): no filename provided");
      MLOUTSTREAM = NULL;
   }
   else
   {
      LOG_INFO("macrologger_main.h:openMLOutstream(): opening %s for logging",
                logfilename);
      FILE *f = fopen(logfilename, "a");

      if (f)
        MLOUTSTREAM = f;    // success
      else
      {
         LOG_ERROR("macrologger_main.h:openMLOutstream(): failed to open %s",
            logfilename);
         perror("macrologger_main.h:openMLOutstream() ");
         MLOUTSTREAM = NULL;
      }
   }

   return MLOUTSTREAM;
}

/*
 * char setMLLevel(const char loglevel)
 *
 *   If loglevel is a valid value between NO_LOG and DEBUG_LEVEL, set
 *   the logging level to loglevel. Otherwise, the level is unchanged.
 *
 *   Returns the value of the logging level.
 */

static inline char setMLLevel(const char loglevel)
{
   LOG_INFO(
        "macrologger_main.h:setMLLevel(): setting log level from %0x to %0x",
        MLLOGLEVEL, loglevel);

   MLLOGLEVEL = (loglevel <= DEBUG_LEVEL && loglevel >= NO_LOG) ? loglevel :
                                                    /* no op */   MLLOGLEVEL;

   LOG_INFO("macrologger_main.h:setMLLevel(): log level is %0x",
        MLLOGLEVEL);

   return MLLOGLEVEL;
}

/*
 * char setMLVerbose(const char verbose)
 *
 *   If verbose is not 0, set MLVERBOSE to true. Otherwise, set verbose false.
 *
 *   Returns the value of the MLVERBOSE.
 */

static inline char setMLVerbose(const char verbose)
{
   LOG_INFO(
        "macrologger_main.h:setMLVerbose(): setting MLVERBOSE from %0x to %0x",
        MLVERBOSE, verbose);

   MLVERBOSE = (verbose != 0) ? 1 : 0;

   LOG_INFO("macrologger_main.h:setMLVerbose(): MLVERBOSE is %0x",
        MLVERBOSE);

   return MLVERBOSE;
}
#endif
