/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS is free software. You can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * OpenPBS 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 Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
 * Altair Legal Department.
 *
 * Altair's dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of OpenPBS and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair's trademarks, including but not limited to "PBS™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

/**
 * @file	log_event.c
 * @brief
 * log_event.c - contains functions to log event messages to the log file.
 *
 *	This is specific to the PBS Server.
 *
 * @par Functions included are:
 *	log_event()
 *	log_change()
 */

#include <pbs_config.h> /* the master config generated by configure */

#include <sys/param.h>
#include <sys/types.h>
#include "pbs_ifl.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "log.h"
#include "server_limits.h"
#include "list_link.h"
#include "attribute.h"
#include "server.h"
#include "libutil.h"

/* private data */

static long log_event_lvl_priv = PBSEVENT_ERROR | PBSEVENT_SYSTEM |
				 PBSEVENT_ADMIN | PBSEVENT_JOB | PBSEVENT_SCHED |
				 PBSEVENT_JOB_USAGE | PBSEVENT_SECURITY |
				 PBSEVENT_DEBUG | PBSEVENT_DEBUG2 |
				 PBSEVENT_RESV;

/* external global data */

extern char *path_home;
long *log_event_mask = &log_event_lvl_priv;

/**
 * @brief
 * 	Checks whether or not the given event type is being recorded into
 *	log file.
 *
 * @param[in] eventtype - event type
 *
 * @return int
 *	1 - if the given event type gets logged,
 *	0 - otherwise, it's not recorded.
 *
 */
int
will_log_event(int eventtype)
{
	if (((eventtype & PBSEVENT_FORCE) != 0) ||
	    ((*log_event_mask & eventtype) != 0))
		return (1);

	return (0);
}
/**
 * @brief
 * 	log_event - log a server event to the log file
 *
 *	Checks to see if the event type is being recorded.  If they are,
 *	pass off to log_record().
 *
 *	The caller should ensure proper formating of the message if "text"
 *	is to contain "continuation lines".
 *
 * @param[in] eventtype - event type
 * @param[in] objclass - event object class
 * @param[in] sev - indication for whether to syslogging enabled or not
 * @param[in] objname - object name stating log msg related to which object
 * @param[in] text - log msg to be logged.
 *
 *	Note, "sev" or severity is used only if syslogging is enabled,
 *	see syslog(3) and log_record.c for details.
 */

void
log_event(int eventtype, int objclass, int sev, const char *objname, const char *text)
{
	if (will_log_event(eventtype))
		log_record(eventtype, objclass, sev, objname, text);
}

/**
 * @brief
 *      do_log_eventf - helper function which does the actual logging
 *
 * @param[in] eventtype - event type
 * @param[in] objclass - event object class
 * @param[in] sev - indication for whether to syslogging enabled or not
 * @param[in] objname - object name stating log msg related to which object
 * @param[in] fmt - format string
 * @param[in] ... - arguments to format string
 *
 * @return void
 */

void
do_log_eventf(int eventtype, int objclass, int sev, const char *objname, const char *fmt, va_list args)
{
	va_list args_copy;
	int len;
	char logbuf[LOG_BUF_SIZE];
	char *buf;

	if (will_log_event(eventtype) == 0)
		return;

	va_copy(args_copy, args);

	len = vsnprintf(logbuf, sizeof(logbuf), fmt, args_copy);
	va_end(args_copy);

	if (len >= sizeof(logbuf)) {
		buf = pbs_asprintf_format(len, fmt, args);
		if (buf == NULL)
			return;

	} else
		buf = logbuf;

	log_record(eventtype, objclass, sev, objname, buf);

	if (len >= sizeof(logbuf))
		free(buf);
}

/**
 * @brief
 * 	log_eventf - a combination of log_event() and printf()
 *
 * @param[in] eventtype - event type
 * @param[in] objclass - event object class
 * @param[in] sev - indication for whether to syslogging enabled or not
 * @param[in] objname - object name stating log msg related to which object
 * @param[in] fmt - format string
 * @param[in] ... - arguments to format string
 *
 * @return void
 */
void
log_eventf(int eventtype, int objclass, int sev, const char *objname, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	do_log_eventf(eventtype, objclass, sev, objname, fmt, args);
	va_end(args);
}
