getrtlogs.c

Exemplo de recepção de logs em tempo real.

Veja Nota sobre os exemplos.
#include <windows.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "10.10.4.13"
#define USER_ID "master"
#define USER_PWD "12345678"
#define FILE_NAME "./Dfence.log"
int AAP_API ReceiveLogsCallback(char *szEvent, void * pParam, BOOL bFinal)
{
int nRet = 0;
FILE *pFile = (FILE *)pParam;
if (pFile){
nRet = fprintf(pFile, "%s\n", szEvent);
//Imprime o registro também na console
nRet = fprintf(stdout, "%s\n", szEvent);
}
if (bFinal){
fclose(pFile);
pFile = NULL;
}
return nRet;
}
void main()
{
int nRet;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
FILE *pFile = NULL;
//Inicializa as bibliotecas do <%HSM_NAME%>
nRet = DInitialize(0);
if (nRet){
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n",nRet);
exit(1);
}
printf("Bibliotecas inicializadas.\n");
//Inicializa a estrutura para conexão com o HSM
strncpy(authPwd.szAddr, HOST_ADDR, sizeof(authPwd.szAddr));
authPwd.nPort = DEFAULT_PORT;
strncpy(authPwd.szUserId, USER_ID, sizeof(authPwd.szUserId));
strncpy(authPwd.szPassword, USER_PWD, sizeof(authPwd.szPassword));
nRet = DOpenSession(&hSession, SS_USER_PWD, (BYTE *)&authPwd, sizeof(authPwd), 0);
if (nRet){
printf("Falha na funcao: DOpenSession \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Sessao com o HSM estabelecida.\n");
pFile = fopen(FILE_NAME, "wt");
if (pFile == NULL){
printf("Falha na funcao: fopen \nCodigo de erro: %d\n", errno);
goto clean;
}
printf("----INICIO----");
nRet = DGetLogEvents(hSession, ReceiveLogsCallback, pFile);
if (nRet){
printf("Falha na funcao: DGetLogEvents \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("----FIM----");
clean:
if (pFile){
fclose(pFile);
printf("Arquivo de log fechado.\n");
}
if (hSession) {
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizada.\n");
}