logging.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // libtgvoip is free and unencumbered public domain software.
  3. // For more information, see http://unlicense.org or the UNLICENSE file
  4. // you should have received with this source code distribution.
  5. //
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <time.h>
  9. #include "VoIPController.h"
  10. #ifdef __ANDROID__
  11. #include <sys/system_properties.h>
  12. #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  13. #include <sys/utsname.h>
  14. #endif
  15. #ifdef __APPLE__
  16. #include <TargetConditionals.h>
  17. #include "os/darwin/DarwinSpecific.h"
  18. #endif
  19. FILE* tgvoipLogFile=NULL;
  20. void tgvoip_log_file_printf(char level, const char* msg, ...){
  21. if(tgvoipLogFile){
  22. va_list argptr;
  23. va_start(argptr, msg);
  24. time_t t = time(0);
  25. struct tm *now = localtime(&t);
  26. fprintf(tgvoipLogFile, "%02d-%02d %02d:%02d:%02d %c: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, level);
  27. vfprintf(tgvoipLogFile, msg, argptr);
  28. fprintf(tgvoipLogFile, "\n");
  29. fflush(tgvoipLogFile);
  30. }
  31. }
  32. void tgvoip_log_file_write_header(FILE* file){
  33. if(file){
  34. time_t t = time(0);
  35. struct tm *now = localtime(&t);
  36. #if defined(_WIN32)
  37. #if WINAPI_PARTITION_DESKTOP
  38. char systemVersion[64];
  39. OSVERSIONINFOA vInfo;
  40. vInfo.dwOSVersionInfoSize=sizeof(vInfo);
  41. GetVersionExA(&vInfo);
  42. snprintf(systemVersion, sizeof(systemVersion), "Windows %d.%d.%d %s", vInfo.dwMajorVersion, vInfo.dwMinorVersion, vInfo.dwBuildNumber, vInfo.szCSDVersion);
  43. #else
  44. char* systemVersion="Windows RT";
  45. #endif
  46. #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  47. #ifdef __ANDROID__
  48. char systemVersion[128];
  49. char sysRel[PROP_VALUE_MAX];
  50. char deviceVendor[PROP_VALUE_MAX];
  51. char deviceModel[PROP_VALUE_MAX];
  52. __system_property_get("ro.build.version.release", sysRel);
  53. __system_property_get("ro.product.manufacturer", deviceVendor);
  54. __system_property_get("ro.product.model", deviceModel);
  55. snprintf(systemVersion, sizeof(systemVersion), "Android %s (%s %s)", sysRel, deviceVendor, deviceModel);
  56. #else
  57. struct utsname sysname;
  58. uname(&sysname);
  59. std::string sysver(sysname.sysname);
  60. sysver+=" ";
  61. sysver+=sysname.release;
  62. sysver+=" (";
  63. sysver+=sysname.version;
  64. sysver+=")";
  65. const char* systemVersion=sysver.c_str();
  66. #endif
  67. #elif defined(__APPLE__)
  68. char osxVer[128];
  69. tgvoip::DarwinSpecific::GetSystemName(osxVer, sizeof(osxVer));
  70. char systemVersion[128];
  71. #if TARGET_OS_OSX
  72. snprintf(systemVersion, sizeof(systemVersion), "OS X %s", osxVer);
  73. #elif TARGET_OS_IPHONE
  74. snprintf(systemVersion, sizeof(systemVersion), "iOS %s", osxVer);
  75. #else
  76. snprintf(systemVersion, sizeof(systemVersion), "Unknown Darwin %s", osxVer);
  77. #endif
  78. #else
  79. const char* systemVersion="Unknown OS";
  80. #endif
  81. #if defined(__aarch64__)
  82. const char* cpuArch="ARM64";
  83. #elif defined(__arm__) || defined(_M_ARM)
  84. const char* cpuArch="ARM";
  85. #elif defined(_M_X64) || defined(__x86_64__)
  86. const char* cpuArch="x86_64";
  87. #elif defined(_M_IX86) || defined(__i386__)
  88. const char* cpuArch="x86";
  89. #else
  90. const char* cpuArch="Unknown CPU";
  91. #endif
  92. fprintf(file, "---------------\nlibtgvoip v" LIBTGVOIP_VERSION " on %s %s\nLog started on %d/%02d/%d at %d:%02d:%02d\n---------------\n", systemVersion, cpuArch, now->tm_mday, now->tm_mon+1, now->tm_year+1900, now->tm_hour, now->tm_min, now->tm_sec);
  93. }
  94. }