19 Star 39 Fork 46

openGauss / openGauss-connector-odbc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mylog.c 11.80 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
/*-------
* Module: mylog.c
*
* Description: This module contains miscellaneous routines
* such as for debugging/logging and string functions.
*
* Classes: n/a
*
* API functions: none
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#define _MYLOG_FUNCS_IMPLEMENT_
#include "psqlodbc.h"
#include "dlg_specific.h"
#include "misc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifndef WIN32
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#define GENERAL_ERRNO (errno)
#define GENERAL_ERRNO_SET(e) (errno = e)
#else
#define GENERAL_ERRNO (GetLastError())
#define GENERAL_ERRNO_SET(e) SetLastError(e)
#include <process.h> /* Byron: is this where Windows keeps def.
* of getpid ? */
#endif
#ifdef WIN32
#define DIRSEPARATOR "\\"
#define PG_BINARY O_BINARY
#define PG_BINARY_R "rb"
#define PG_BINARY_W "wb"
#define PG_BINARY_A "ab"
#else
#define DIRSEPARATOR "/"
#define PG_BINARY 0
#define PG_BINARY_R "r"
#define PG_BINARY_W "w"
#define PG_BINARY_A "a"
#endif /* WIN32 */
static char *logdir = NULL;
void
generate_filename(const char *dirname, const char *prefix, char *filename, size_t filenamelen)
{
const char *exename = GetExeProgramName();
#ifdef WIN32
int pid;
pid = _getpid();
#else
pid_t pid;
struct passwd *ptr;
ptr = getpwuid(getuid());
if (NULL == ptr)
return;
pid = getpid();
#endif
if (dirname == 0 || filename == 0)
return;
snprintf(filename, filenamelen, "%s%s", dirname, DIRSEPARATOR);
if (prefix != 0)
strlcat(filename, prefix, filenamelen);
if (exename[0])
snprintfcat(filename, filenamelen, "%s_", exename);
#ifndef WIN32
if (ptr)
strlcat(filename, ptr->pw_name, filenamelen);
#endif
snprintfcat(filename, filenamelen, "%u%s", pid, ".log");
return;
}
static void
generate_homefile(const char *prefix, char *filename, size_t filenamelen)
{
char dir[PATH_MAX];
#ifdef WIN32
const char *ptr;
dir[0] = '\0';
if (ptr=getenv("HOMEDRIVE"), NULL != ptr)
strlcat(dir, ptr, filenamelen);
if (ptr=getenv("HOMEPATH"), NULL != ptr)
strlcat(dir, ptr, filenamelen);
#else
STRCPY_FIXED(dir, "~");
#endif /* WIN32 */
generate_filename(dir, prefix, filename, filenamelen);
return;
}
#ifdef WIN32
static char exename[_MAX_FNAME];
#elif defined MAXNAMELEN
static char exename[MAXNAMELEN];
#else
static char exename[256];
#endif
const char *GetExeProgramName()
{
static int init = 1;
if (init)
{
UCHAR *p;
#ifdef WIN32
char pathname[_MAX_PATH];
if (GetModuleFileName(NULL, pathname, sizeof(pathname)) > 0)
_splitpath(pathname, NULL, NULL, exename, NULL);
#else
CSTR flist[] = {"/proc/self/exe", "/proc/curproc/file", "/proc/curproc/exe" };
int i;
char path_name[256];
for (i = 0; i < sizeof(flist) / sizeof(flist[0]); i++)
{
if (readlink(flist[i], path_name, sizeof(path_name)) > 0)
{
STRCPY_FIXED(exename, po_basename(path_name));
break;
}
}
#endif /* WIN32 */
for (p = (UCHAR *) exename; '\0' != *p; p++)
{
if (isalnum(*p))
continue;
switch (*p)
{
case '_':
case '-':
continue;
}
*p = '\0'; /* avoid multi bytes for safety */
break;
}
init = 0;
}
return exename;
}
#if defined(WIN_MULTITHREAD_SUPPORT)
static CRITICAL_SECTION qlog_cs, mylog_cs;
#elif defined(POSIX_MULTITHREAD_SUPPORT)
static pthread_mutex_t qlog_cs, mylog_cs;
#endif /* WIN_MULTITHREAD_SUPPORT */
static int mylog_on = 0, qlog_on = 0;
#if defined(WIN_MULTITHREAD_SUPPORT)
#define INIT_QLOG_CS InitializeCriticalSection(&qlog_cs)
#define ENTER_QLOG_CS EnterCriticalSection(&qlog_cs)
#define LEAVE_QLOG_CS LeaveCriticalSection(&qlog_cs)
#define DELETE_QLOG_CS DeleteCriticalSection(&qlog_cs)
#define INIT_MYLOG_CS InitializeCriticalSection(&mylog_cs)
#define ENTER_MYLOG_CS EnterCriticalSection(&mylog_cs)
#define LEAVE_MYLOG_CS LeaveCriticalSection(&mylog_cs)
#define DELETE_MYLOG_CS DeleteCriticalSection(&mylog_cs)
#elif defined(POSIX_MULTITHREAD_SUPPORT)
#define INIT_QLOG_CS pthread_mutex_init(&qlog_cs,0)
#define ENTER_QLOG_CS pthread_mutex_lock(&qlog_cs)
#define LEAVE_QLOG_CS pthread_mutex_unlock(&qlog_cs)
#define DELETE_QLOG_CS pthread_mutex_destroy(&qlog_cs)
#define INIT_MYLOG_CS pthread_mutex_init(&mylog_cs,0)
#define ENTER_MYLOG_CS pthread_mutex_lock(&mylog_cs)
#define LEAVE_MYLOG_CS pthread_mutex_unlock(&mylog_cs)
#define DELETE_MYLOG_CS pthread_mutex_destroy(&mylog_cs)
#else
#define INIT_QLOG_CS
#define ENTER_QLOG_CS
#define LEAVE_QLOG_CS
#define DELETE_QLOG_CS
#define INIT_MYLOG_CS
#define ENTER_MYLOG_CS
#define LEAVE_MYLOG_CS
#define DELETE_MYLOG_CS
#endif /* WIN_MULTITHREAD_SUPPORT */
#define MYLOGFILE "mylog_"
#ifndef WIN32
#define MYLOGDIR "/tmp"
#else
#define MYLOGDIR "c:"
#endif /* WIN32 */
#define QLOGFILE "psqlodbc_"
#ifndef WIN32
#define QLOGDIR "/tmp"
#else
#define QLOGDIR "c:"
#endif /* WIN32 */
int get_mylog(void)
{
return mylog_on;
}
int get_qlog(void)
{
return qlog_on;
}
const char *po_basename(const char *path)
{
char *p;
if (p = strrchr(path, DIRSEPARATOR[0]), NULL != p)
return p + 1;
return path;
}
void
logs_on_off(int cnopen, int mylog_onoff, int qlog_onoff)
{
static int mylog_on_count = 0,
mylog_off_count = 0,
qlog_on_count = 0,
qlog_off_count = 0;
ENTER_MYLOG_CS;
if (mylog_onoff)
mylog_on_count += cnopen;
else
mylog_off_count += cnopen;
if (mylog_on_count > 0)
{
if (mylog_onoff > mylog_on)
mylog_on = mylog_onoff;
else if (mylog_on < 1)
mylog_on = 1;
}
else if (mylog_off_count > 0)
mylog_on = 0;
else if (getGlobalDebug() > 0)
mylog_on = getGlobalDebug();
LEAVE_MYLOG_CS;
ENTER_QLOG_CS;
if (qlog_onoff)
qlog_on_count += cnopen;
else
qlog_off_count += cnopen;
if (qlog_on_count > 0)
{
if (qlog_onoff > qlog_on)
qlog_on = qlog_onoff;
else if (qlog_on < 1)
qlog_on = 1;
}
else if (qlog_off_count > 0)
qlog_on = 0;
else if (getGlobalCommlog() > 0)
qlog_on = getGlobalCommlog();
LEAVE_QLOG_CS;
MYLOG(0, "mylog_on=%d qlog_on=%d\n", mylog_on, qlog_on);
}
#ifdef WIN32
#define LOGGING_PROCESS_TIME
#include <direct.h>
#endif /* WIN32 */
#ifdef LOGGING_PROCESS_TIME
#include <mmsystem.h>
static DWORD start_time = 0;
#endif /* LOGGING_PROCESS_TIME */
static FILE *MLOGFP = NULL;
static void MLOG_open()
{
char filebuf[80], errbuf[160];
BOOL open_error = FALSE;
if (MLOGFP) return;
generate_filename(logdir ? logdir : MYLOGDIR, MYLOGFILE, filebuf, sizeof(filebuf));
MLOGFP = fopen(filebuf, PG_BINARY_A);
if (!MLOGFP)
{
int lasterror = GENERAL_ERRNO;
open_error = TRUE;
SPRINTF_FIXED(errbuf, "%s open error %d\n", filebuf, lasterror);
generate_homefile(MYLOGFILE, filebuf, sizeof(filebuf));
MLOGFP = fopen(filebuf, PG_BINARY_A);
}
if (MLOGFP)
{
setbuf(MLOGFP, NULL);
if (open_error)
fputs(errbuf, MLOGFP);
}
}
static int
mylog_misc(unsigned int option, const char *fmt, va_list args)
{
// va_list args;
int gerrno;
BOOL log_threadid = option;
gerrno = GENERAL_ERRNO;
ENTER_MYLOG_CS;
#ifdef LOGGING_PROCESS_TIME
if (!start_time)
start_time = timeGetTime();
#endif /* LOGGING_PROCESS_TIME */
if (!MLOGFP)
{
MLOG_open();
if (!MLOGFP)
mylog_on = 0;
}
if (MLOGFP)
{
if (log_threadid)
{
#ifdef WIN_MULTITHREAD_SUPPORT
#ifdef LOGGING_PROCESS_TIME
DWORD proc_time = timeGetTime() - start_time;
fprintf(MLOGFP, "[%u-%d.%03d]", GetCurrentThreadId(), proc_time / 1000, proc_time % 1000);
#else
fprintf(MLOGFP, "[%u]", GetCurrentThreadId());
#endif /* LOGGING_PROCESS_TIME */
#endif /* WIN_MULTITHREAD_SUPPORT */
#if defined(POSIX_MULTITHREAD_SUPPORT)
fprintf(MLOGFP, "[%lx]", (unsigned long int) pthread_self());
#endif /* POSIX_MULTITHREAD_SUPPORT */
}
vfprintf(MLOGFP, fmt, args);
}
LEAVE_MYLOG_CS;
GENERAL_ERRNO_SET(gerrno);
return 1;
}
DLL_DECLARE int
mylog(const char *fmt,...)
{
int ret = 0;
unsigned int option = 1;
va_list args;
if (!mylog_on) return ret;
va_start(args, fmt);
ret = mylog_misc(option, fmt, args);
va_end(args);
return ret;
}
DLL_DECLARE int
myprintf(const char *fmt,...)
{
int ret = 0;
va_list args;
va_start(args, fmt);
ret = mylog_misc(0, fmt, args);
va_end(args);
return ret;
}
static void mylog_initialize(void)
{
INIT_MYLOG_CS;
}
static void mylog_finalize(void)
{
ENTER_MYLOG_CS;
mylog_on = 0;
if (MLOGFP)
{
fclose(MLOGFP);
MLOGFP = NULL;
}
DELETE_MYLOG_CS;
}
static FILE *QLOGFP = NULL;
static int
qlog_misc(unsigned int option, const char *fmt, va_list args)
{
char filebuf[80];
int gerrno;
if (!qlog_on) return 0;
gerrno = GENERAL_ERRNO;
ENTER_QLOG_CS;
#ifdef LOGGING_PROCESS_TIME
if (!start_time)
start_time = timeGetTime();
#endif /* LOGGING_PROCESS_TIME */
if (!QLOGFP)
{
generate_filename(logdir ? logdir : QLOGDIR, QLOGFILE, filebuf, sizeof(filebuf));
QLOGFP = fopen(filebuf, PG_BINARY_A);
if (!QLOGFP)
{
generate_homefile(QLOGFILE, filebuf, sizeof(filebuf));
QLOGFP = fopen(filebuf, PG_BINARY_A);
}
if (QLOGFP)
setbuf(QLOGFP, NULL);
else
qlog_on = 0;
}
if (QLOGFP)
{
if (option)
{
#ifdef LOGGING_PROCESS_TIME
DWORD proc_time = timeGetTime() - start_time;
fprintf(QLOGFP, "[%d.%03d]", proc_time / 1000, proc_time % 1000);
#endif /* LOGGING_PROCESS_TIME */
}
vfprintf(QLOGFP, fmt, args);
}
LEAVE_QLOG_CS;
GENERAL_ERRNO_SET(gerrno);
return 1;
}
int
qlog(char *fmt,...)
{
int ret = 0;
unsigned int option = 1;
va_list args;
if (!qlog_on) return ret;
va_start(args, fmt);
ret = qlog_misc(option, fmt, args);
va_end(args);
return ret;
}
int
qprintf(char *fmt,...)
{
int ret = 0;
va_list args;
va_start(args, fmt);
ret = qlog_misc(0, fmt, args);
va_end(args);
return ret;
}
static void qlog_initialize(void)
{
INIT_QLOG_CS;
}
static void qlog_finalize(void)
{
ENTER_QLOG_CS;
qlog_on = 0;
if (QLOGFP)
{
fclose(QLOGFP);
QLOGFP = NULL;
}
DELETE_QLOG_CS;
}
static int globalDebug = -1;
int
getGlobalDebug()
{
char temp[16];
if (globalDebug >=0)
return globalDebug;
/* Debug is stored in the driver section */
SQLGetPrivateProfileString(DBMS_NAME, INI_DEBUG, "", temp, sizeof(temp), ODBCINST_INI);
if (temp[0])
globalDebug = atoi(temp);
else
globalDebug = DEFAULT_DEBUG;
return globalDebug;
}
int
setGlobalDebug(int val)
{
return (globalDebug = val);
}
static int globalCommlog = -1;
int
getGlobalCommlog()
{
char temp[16];
if (globalCommlog >= 0)
return globalCommlog;
/* Commlog is stored in the driver section */
SQLGetPrivateProfileString(DBMS_NAME, INI_COMMLOG, "", temp, sizeof(temp), ODBCINST_INI);
if (temp[0])
globalCommlog = atoi(temp);
else
globalCommlog = DEFAULT_COMMLOG;
return globalCommlog;
}
int
setGlobalCommlog(int val)
{
return (globalCommlog = val);
}
int
writeGlobalLogs()
{
char temp[10];
ITOA_FIXED(temp, globalDebug);
SQLWritePrivateProfileString(DBMS_NAME, INI_DEBUG, temp, ODBCINST_INI);
ITOA_FIXED(temp, globalCommlog);
SQLWritePrivateProfileString(DBMS_NAME, INI_COMMLOG, temp, ODBCINST_INI);
return 0;
}
int
getLogDir(char *dir, int dirmax)
{
return SQLGetPrivateProfileString(DBMS_NAME, INI_LOGDIR, "", dir, dirmax, ODBCINST_INI);
}
int
setLogDir(const char *dir)
{
return SQLWritePrivateProfileString(DBMS_NAME, INI_LOGDIR, dir, ODBCINST_INI);
}
/*
* This function starts a logging out of connections according the ODBCINST.INI
* portion of the DBMS_NAME registry.
*/
static void
start_logging()
{
/*
* GlobalDebug or GlobalCommlog means whether take mylog or commlog
* out of the connection time or not but doesn't mean the default of
* ci->drivers.debug(commlog).
*/
logs_on_off(0, 0, 0);
mylog("\t%s:Global.debug&commlog=%d&%d\n", __FUNCTION__, getGlobalDebug(), getGlobalCommlog());
}
void InitializeLogging(void)
{
char dir[PATH_MAX];
getLogDir(dir, sizeof(dir));
if (dir[0])
logdir = strdup(dir);
mylog_initialize();
qlog_initialize();
start_logging();
}
void FinalizeLogging(void)
{
mylog_finalize();
qlog_finalize();
if (logdir)
{
free(logdir);
logdir = NULL;
}
}
C
1
https://gitee.com/opengauss/openGauss-connector-odbc.git
git@gitee.com:opengauss/openGauss-connector-odbc.git
opengauss
openGauss-connector-odbc
openGauss-connector-odbc
master

搜索帮助