Examples of Logging APIs Used in a C++ Agent
#include <stdio.h>
#include <locale.h>
#include "VCSAgApi.h"
void file_attr_changed(const char *res_name, const char
*changed_res_name,const char *changed_attr_name, void
**new_val)
{
/*
* NOT REQUIRED if the function is empty or is not logging
* any messages to the agent log file
*/
VCSAG_LOG_INIT("file_attr_changed");
}
extern "C" unsigned int
file_clean(const char *res_name, VCSAgWhyClean wc, void
**attr_val)
{
VCSAG_LOG_INIT("file_clean");
if ((attr_val) && (*attr_val)) {
if ((remove((CHAR *)(*attr_val)) == 0) || (errno ==
ENOENT)) { return 0; // Success
}
}
return 1; // Failure
}
void file_close(const char *res_name, void **attr_val)
{
VCSAG_LOG_INIT("file_close");
}
//
// Determine if the given file is online (file exists) or
// offline (file does not exist).
//
extern "C" VCSAgResState
file_monitor(const char *res_name, void **attr_val, int *conf_level)
{
VCSAG_LOG_INIT("file_monitor");
VCSAgResState state = VCSAgResUnknown;
*conf_level = 0;
/*
* This msg will be printed for all resources if VCS_DBG4
* is enabled for the resource type. Else it will be
* logged only for that resource that has the dbg level
* VCS_DBG4 enabled
*/
VCSAG_RES_LOG_MSG(VCS_DBG4, VCS_DEFAULT_FLAGS, "PathName is
(%s)", (CHAR *)(*attr_val));
if ((attr_val) && (*attr_val)) {
struct stat stat_buf;
if ( (stat((CHAR *)(* attr_val), &stat_buf) == 0)
&& (strlen((CHAR *)(* attr_val)) != 0) ) {
state = VCSAgResOnline; *conf_level = 100;
}
else {
state = VCSAgResOffline;
*conf_level = 0;
}
}
VCSAG_RES_LOG_MSG(VCS_DBG7, VCS_DEFAULT_FLAGS, "State is
(%d)", (int)state);
return state;
}
extern "C" unsigned int
file_online(const char *res_name, void **attr_val) {
int fd = -1;
VCSAG_LOG_INIT("file_online");
if ((attr_val) && (*attr_val)) {
if (strlen((CHAR *)(* attr_val)) == 0) {
VCSAG_LOG_MSG(VCS_WARNING, 3001, VCS_DEFAULT_FLAGS,
"The value for PathName attribute is not specified");
VCSAG_CONSOLE_LOG_MSG(VCS_WARNING, 3001,
VCS_DEFAULT_FLAGS,
"The value for PathName attribute is not specified");
return 0;
}
if (fd = creat((CHAR *)(*attr_val), S_IRUSR|S_IWUSR) < 0) {
VCSAG_LOG_MSG(VCS_ERROR, 3002, VCS_DEFAULT_FLAGS,
"Resource could not be brought up because, "
"the attempt to create the file(%s) failed "
"with error(%d)", (CHAR *)(*attr_val), errno);
VCSAG_CONSOLE_LOG_MSG(VCS_ERROR, 3002,
VCS_DEFAULT_FLAGS,
"Resource could not be brought up because, "
"the attempt to create the file(%s) failed "
"with error(%d)", (CHAR *)(*attr_val), errno);
return 0;
}
close(fd);
}
return 0;
}
extern "C" unsigned int
file_offline(const char *res_name, void **attr_val)
{
VCSAG_LOG_INIT("file_offline");
if ((attr_val) && (*attr_val) && (remove((CHAR*)
(*attr_val)) != 0) && (errno != ENOENT)) {
VCSAG_LOG_MSG(VCS_ERROR, 14002, VCS_DEFAULT_FLAGS,
"Resource could not be brought down because, the
attempt to remove the file(%s) failed with
error(%d)", (CHAR *)(*attr_val), errno);
VCSAG_CONSOLE_LOG_MSG(VCS_ERROR, 14002,
VCS_DEFAULT_FLAGS, "Resource could not be brought
down because, the attempt to remove the file(%s)
failed with error(%d)", (CHAR *)(*attr_val), errno);
}
return 0;
}
void file_open(const char *res_name, void **attr_val)
{
VCSAG_LOG_INIT("file_open");
}
VCSEXPORT void VCSDECL VCSAgStartup()
{
VCSAG_LOG_INIT("VCSAgStartup");
VCSAgV40EntryPointStruct ep;
ep.open = NULL;
ep.close = NULL;
ep.monitor = file_monitor;
ep.online = file_online;
ep.offline = file_offline;
ep.clean = file_clean;
ep.attr_changed = NULL;
ep.shutdown = NULL;
ep.action = NULL;
ep.info = NULL;
VCSAgSetLogCategory(2001);
char *s = setlocale(LC_ALL, NULL);
VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS, "Locale is
%s", s);
VCSAgRegisterEPStruct(V40, &ep);
}
|