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 
7 
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <time.h>
11 
12 #include "VoIPController.h"
13 
14 #ifdef __ANDROID__
15 #include <sys/system_properties.h>
16 #elif defined(__linux__) || defined(__FreeBSD__)
17 #include <sys/utsname.h>
18 #endif
19 
20 #ifdef __APPLE__
21 #include <TargetConditionals.h>
22 #include "os/darwin/DarwinSpecific.h"
23 #endif
24 
25 FILE* tgvoipLogFile=NULL;
26 
tgvoip_log_file_printf(char level,const char * msg,...)27 void tgvoip_log_file_printf(char level, const char* msg, ...){
28 	if(tgvoipLogFile){
29 		va_list argptr;
30 		va_start(argptr, msg);
31 		time_t t = time(0);
32 		struct tm *now = localtime(&t);
33 		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);
34 		vfprintf(tgvoipLogFile, msg, argptr);
35 		fprintf(tgvoipLogFile, "\n");
36 		fflush(tgvoipLogFile);
37 	}
38 }
39 
tgvoip_log_file_write_header(FILE * file)40 void tgvoip_log_file_write_header(FILE* file){
41 	if(file){
42 		time_t t = time(0);
43 		struct tm *now = localtime(&t);
44 #if defined(_WIN32)
45 		#if WINAPI_PARTITION_DESKTOP
46 			char systemVersion[64];
47 			OSVERSIONINFOA vInfo;
48 			vInfo.dwOSVersionInfoSize=sizeof(vInfo);
49 			GetVersionExA(&vInfo);
50 			snprintf(systemVersion, sizeof(systemVersion), "Windows %d.%d.%d %s", vInfo.dwMajorVersion, vInfo.dwMinorVersion, vInfo.dwBuildNumber, vInfo.szCSDVersion);
51 #else
52 			char* systemVersion="Windows RT";
53 #endif
54 #elif defined(__linux__) || defined(__FreeBSD__)
55 #ifdef __ANDROID__
56 		char systemVersion[128];
57 		char sysRel[PROP_VALUE_MAX];
58 		char deviceVendor[PROP_VALUE_MAX];
59 		char deviceModel[PROP_VALUE_MAX];
60 		__system_property_get("ro.build.version.release", sysRel);
61 		__system_property_get("ro.product.manufacturer", deviceVendor);
62 		__system_property_get("ro.product.model", deviceModel);
63 		snprintf(systemVersion, sizeof(systemVersion), "Android %s (%s %s)", sysRel, deviceVendor, deviceModel);
64 #else
65 		struct utsname sysname;
66 		uname(&sysname);
67 		std::string sysver(sysname.sysname);
68 		sysver+=" ";
69 		sysver+=sysname.release;
70 		sysver+=" (";
71 		sysver+=sysname.version;
72 		sysver+=")";
73 		const char* systemVersion=sysver.c_str();
74 #endif
75 #elif defined(__APPLE__)
76 		char osxVer[128];
77 		tgvoip::DarwinSpecific::GetSystemName(osxVer, sizeof(osxVer));
78 		char systemVersion[128];
79 #if TARGET_OS_OSX
80 		snprintf(systemVersion, sizeof(systemVersion), "OS X %s", osxVer);
81 #elif TARGET_OS_IPHONE
82 		snprintf(systemVersion, sizeof(systemVersion), "iOS %s", osxVer);
83 #else
84 		snprintf(systemVersion, sizeof(systemVersion), "Unknown Darwin %s", osxVer);
85 #endif
86 #else
87 		const char* systemVersion="Unknown OS";
88 #endif
89 
90 #if defined(__aarch64__)
91 		const char* cpuArch="ARM64";
92 #elif defined(__arm__) || defined(_M_ARM)
93 		const char* cpuArch="ARM";
94 #elif defined(_M_X64) || defined(__x86_64__)
95 		const char* cpuArch="x86_64";
96 #elif defined(_M_IX86) || defined(__i386__)
97 		const char* cpuArch="x86";
98 #else
99 		const char* cpuArch="Unknown CPU";
100 #endif
101 
102 		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);
103 	}
104 }
105