This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.c
143 lines (139 loc) · 3.05 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
// Copyright (c) 2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include "log.h"
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <VersionHelpers.h>
#include <windows.h>
#endif
#include "s_helpers.h"
int ansiCapable = 0;
void setAnsi()
{
#ifdef _WIN32
// Returns TRUE if 10.0.10586 is GREATER/EQUAL than installed version,
// TRUE=BAD
if (!IsWindowsVersionOrGreater(10, 0, 10586))
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode;
GetConsoleMode(hConsole, &consoleMode);
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (SetConsoleMode(hConsole, consoleMode))
{
ansiCapable = 1;
}
}
#else
char *term;
term = getenv("TERM");
char *upper = NULL;
toUpper((const unsigned char *)term, &upper);
if (upper != NULL &&
(strcmp(upper, TERM_XTERM_256) == 0 || strcmp(upper, TERM_LINUX) == 0))
{
ansiCapable = 1;
free(upper);
}
#endif
}
void hexDump(const unsigned char *buf, const int len, const char *pad, FILE *fd,
const char *tag)
{
printf("%s%s0000: ", tag, pad);
int i;
for (i = 0; i < len; ++i)
{
printf("%02X ", buf[i]);
if ((i + 1) % 8 == 0)
{
fprintf(fd, " ");
}
if ((i + 1) % 16 == 0 && i + 1 != len)
{
fprintf(fd, "\n%s%s%04X: ", tag, pad, i + 1);
}
else if (i + 1 == len)
{
fprintf(fd, "\n");
}
}
}
const char *getNfo()
{
if (ansiCapable)
{
return LOG_NFO_ANSI;
}
else
{
return LOG_NFO;
}
}
const char *getWrn()
{
if (ansiCapable)
{
return LOG_WRN_ANSI;
}
else
{
return LOG_WRN;
}
}
const char *getErr()
{
if (ansiCapable)
{
return LOG_ERR_ANSI;
}
else
{
return LOG_ERR;
}
}
const char *setAttribute(enum Attribute attr)
{
if (ansiCapable)
{
switch (attr)
{
case Red:
return SET_COLOR_RED;
break;
case Green:
return SET_COLOR_GREEN;
break;
case Clear:
return SET_CLEAR;
break;
case Bold:
return SET_BOLD;
break;
case Blink:
return SET_BLINK;
break;
default:
return "";
break;
}
}
else
{
return "";
}
}