[7d3ae3e] | 1 | /* |
---|
| 2 | * Estonian ID card plugin for web browsers |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2010-2011 Codeborne <info@codeborne.com> |
---|
| 5 | * |
---|
| 6 | * This is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This software is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <stdarg.h> |
---|
| 25 | #include "esteid_time.h" |
---|
| 26 | |
---|
| 27 | #ifndef _WIN32 |
---|
| 28 | #include <unistd.h> |
---|
| 29 | #else |
---|
| 30 | #include <share.h> |
---|
| 31 | #include <io.h> |
---|
| 32 | #define W_OK 2 |
---|
| 33 | #define access _access |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | #include "esteid_log.h" |
---|
| 37 | |
---|
| 38 | #ifdef _WIN32 |
---|
| 39 | #define MAX_LOG_FILE_NAME_LEN _MAX_ENV + 12 |
---|
| 40 | char filename[MAX_LOG_FILE_NAME_LEN] = ""; |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | const char *EstEID_getLogFilename() { |
---|
| 44 | #ifdef _WIN32 |
---|
| 45 | if (!filename[0]) { |
---|
| 46 | char *tempValue; |
---|
| 47 | size_t length; |
---|
| 48 | if(_dupenv_s(&tempValue, &length, "TEMP")) { |
---|
| 49 | tempValue = strdup(""); |
---|
| 50 | } |
---|
| 51 | sprintf_s(filename, MAX_LOG_FILE_NAME_LEN, "%s\\esteid.log", tempValue); |
---|
| 52 | free(tempValue); |
---|
| 53 | } |
---|
| 54 | return filename; |
---|
| 55 | #else |
---|
| 56 | return "/tmp/esteid.log"; |
---|
| 57 | #endif |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | #define TIMESTAMP_BUFFER_LEN 30 |
---|
| 61 | FILE *EstEID_openLog(const char *func, const char *file, int line) { |
---|
| 62 | char timestamp[TIMESTAMP_BUFFER_LEN]; |
---|
| 63 | #ifdef _WIN32 |
---|
| 64 | char delimiter = '\\'; |
---|
| 65 | SYSTEMTIME now; |
---|
| 66 | FILE *log; |
---|
| 67 | GetLocalTime(&now); |
---|
| 68 | sprintf_s(timestamp, TIMESTAMP_BUFFER_LEN, "%d-%02d-%02d %02d:%02d:%02d.%03d", now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond, now.wMilliseconds); |
---|
| 69 | #else |
---|
| 70 | char delimiter = '/'; |
---|
| 71 | struct timeval tv; |
---|
| 72 | time_t curtime; |
---|
| 73 | gettimeofday(&tv, NULL); |
---|
| 74 | curtime = tv.tv_sec; |
---|
| 75 | strftime(timestamp, 30, "%Y-%m-%d %T", localtime(&curtime)); |
---|
| 76 | #ifdef __APPLE__ |
---|
| 77 | sprintf(timestamp + strlen(timestamp), ".%03i ", tv.tv_usec / 1000); |
---|
| 78 | #else |
---|
| 79 | sprintf(timestamp + strlen(timestamp), ".%03li ", tv.tv_usec / 1000); |
---|
| 80 | #endif |
---|
| 81 | #endif |
---|
| 82 | #ifndef _WIN32 |
---|
| 83 | FILE *log = fopen(EstEID_getLogFilename(), "a"); |
---|
| 84 | #else |
---|
| 85 | log = _fsopen(EstEID_getLogFilename(), "a", _SH_DENYNO); |
---|
| 86 | #endif |
---|
| 87 | fprintf(log, "%s ", timestamp); |
---|
| 88 | if (file) { |
---|
| 89 | char *f = strrchr((char *)file, delimiter); |
---|
| 90 | if (!f) f = (char *)file; |
---|
| 91 | else f++; |
---|
| 92 | fprintf(log, "%s() [%s:%i] ", func, f, line); |
---|
| 93 | } |
---|
| 94 | return log; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void EstEID_log_real(const char *func, const char *file, int line, const char *message, ...) { |
---|
| 98 | FILE *log; |
---|
| 99 | va_list args; |
---|
| 100 | |
---|
| 101 | if (access(EstEID_getLogFilename(), W_OK) == -1) { |
---|
| 102 | return; |
---|
| 103 | } |
---|
| 104 | log = EstEID_openLog(func, file, line); |
---|
| 105 | va_start(args, message); |
---|
| 106 | vfprintf(log, message, args); |
---|
| 107 | va_end(args); |
---|
| 108 | fprintf(log, "\n"); |
---|
| 109 | fclose(log); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | //#ifndef WIN_IE |
---|
| 113 | void EstEID_logMap_real(const char *func, const char *file, int line, EstEID_Map map) { |
---|
| 114 | FILE *log; |
---|
| 115 | |
---|
| 116 | if (access(EstEID_getLogFilename(), W_OK) == -1) return; |
---|
| 117 | log = EstEID_openLog(func, file, line); |
---|
| 118 | fprintf(log, "entries:\n"); |
---|
| 119 | EstEID_mapPrint(log, map); |
---|
| 120 | fclose(log); |
---|
| 121 | } |
---|
| 122 | //#endif |
---|