Improve error checking. Add partial support for verbose output

This commit is contained in:
Dr-Noob
2020-06-20 19:21:07 +02:00
parent b81d611282
commit 6ad5f65c34
6 changed files with 61 additions and 19 deletions

View File

@@ -43,6 +43,10 @@ int showVersion() {
return args.version_flag; return args.version_flag;
} }
bool verbose_enabled() {
return false;
}
int parseArgs(int argc, char* argv[]) { int parseArgs(int argc, char* argv[]) {
int c; int c;
int digit_optind = 0; int digit_optind = 0;

View File

@@ -1,11 +1,13 @@
#ifndef __ARGS__ #ifndef __ARGS__
#define __ARGS__ #define __ARGS__
#include <stdbool.h>
#include "printer.h" #include "printer.h"
int parseArgs(int argc, char* argv[]); int parseArgs(int argc, char* argv[]);
STYLE getStyle(); STYLE getStyle();
int showHelp(); int showHelp();
int showVersion(); int showVersion();
bool verbose_enabled();
#endif #endif

View File

@@ -3,8 +3,26 @@
#include "global.h" #include "global.h"
#define RED "\x1b[31;1m" #define RED "\x1b[31;1m"
#define BOLD "\x1b[;1m"
#define RESET "\x1b[0m" #define RESET "\x1b[0m"
#define LOG_LEVEL_NORMAL 0
#define LOG_LEVEL_VERBOSE 1
int LOG_LEVEL;
void printWarn(const char *fmt, ...) {
if(LOG_LEVEL == LOG_LEVEL_VERBOSE) {
int buffer_size = 4096;
char buffer[buffer_size];
va_list args;
va_start(args, fmt);
vsnprintf(buffer,buffer_size, fmt, args);
va_end(args);
fprintf(stderr,BOLD "[WARNING]: "RESET "%s\n",buffer);
}
}
void printErr(const char *fmt, ...) { void printErr(const char *fmt, ...) {
int buffer_size = 4096; int buffer_size = 4096;
char buffer[buffer_size]; char buffer[buffer_size];
@@ -12,7 +30,7 @@ void printErr(const char *fmt, ...) {
va_start(args, fmt); va_start(args, fmt);
vsnprintf(buffer,buffer_size, fmt, args); vsnprintf(buffer,buffer_size, fmt, args);
va_end(args); va_end(args);
fprintf(stderr,RED "ERROR: "RESET "%s\n",buffer); fprintf(stderr,RED "[ERROR]: "RESET "%s\n",buffer);
} }
void printBug(const char *fmt, ...) { void printBug(const char *fmt, ...) {
@@ -22,6 +40,11 @@ void printBug(const char *fmt, ...) {
va_start(args, fmt); va_start(args, fmt);
vsnprintf(buffer,buffer_size, fmt, args); vsnprintf(buffer,buffer_size, fmt, args);
va_end(args); va_end(args);
fprintf(stderr,RED "ERROR: "RESET "%s\n",buffer); fprintf(stderr,RED "[ERROR]: "RESET "%s\n",buffer);
fprintf(stderr,"Please, create a new issue with this error message and your CPU in https://github.com/Dr-Noob/cpufetch/issues\n"); fprintf(stderr,"Please, create a new issue with this error message and your CPU in https://github.com/Dr-Noob/cpufetch/issues\n");
} }
void set_log_level(bool verbose) {
if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE;
else LOG_LEVEL = LOG_LEVEL_NORMAL;
}

View File

@@ -1,6 +1,10 @@
#ifndef __GLOBAL__ #ifndef __GLOBAL__
#define __GLOBAL__ #define __GLOBAL__
#include <stdbool.h>
void set_log_level(bool verbose);
void printWarn(const char *fmt, ...);
void printErr(const char *fmt, ...); void printErr(const char *fmt, ...);
void printBug(const char *fmt, ...); void printBug(const char *fmt, ...);

View File

@@ -24,7 +24,7 @@ Peak FLOPS: 512 GFLOP/s(in simple precision)
***/ ***/
static const char* VERSION = "0.41"; static const char* VERSION = "0.42";
void print_help(int argc, char *argv[]) { void print_help(int argc, char *argv[]) {
printf("Usage: %s [--version] [--help] [--style STYLE]\n\ printf("Usage: %s [--version] [--help] [--style STYLE]\n\
@@ -55,6 +55,8 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
set_log_level(verbose_enabled());
struct cpuInfo* cpu = get_cpu_info(); struct cpuInfo* cpu = get_cpu_info();
if(cpu == NULL) if(cpu == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@@ -83,9 +83,7 @@ void init_cpu_info(struct cpuInfo* cpu) {
} }
#define MASK 0xFF #define MASK 0xFF
VENDOR get_cpu_vendor_internal(unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) { void get_cpu_vendor_internal(char* name, unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) {
char name[13];
memset(name,0,13);
name[__COUNTER__] = ebx & MASK; name[__COUNTER__] = ebx & MASK;
name[__COUNTER__] = (ebx>>8) & MASK; name[__COUNTER__] = (ebx>>8) & MASK;
name[__COUNTER__] = (ebx>>16) & MASK; name[__COUNTER__] = (ebx>>16) & MASK;
@@ -100,14 +98,6 @@ VENDOR get_cpu_vendor_internal(unsigned eax,unsigned ebx,unsigned ecx,unsigned e
name[__COUNTER__] = (ecx>>8) & MASK; name[__COUNTER__] = (ecx>>8) & MASK;
name[__COUNTER__] = (ecx>>16) & MASK; name[__COUNTER__] = (ecx>>16) & MASK;
name[__COUNTER__] = (ecx>>24) & MASK; name[__COUNTER__] = (ecx>>24) & MASK;
if(strcmp(VENDOR_INTEL_STRING,name) == 0)
return VENDOR_INTEL;
else if (strcmp(VENDOR_AMD_STRING,name) == 0)
return VENDOR_AMD;
return VENDOR_INVALID;
} }
struct cpuInfo* get_cpu_info() { struct cpuInfo* get_cpu_info() {
@@ -120,18 +110,26 @@ struct cpuInfo* get_cpu_info() {
unsigned int edx = 0; unsigned int edx = 0;
//Get max cpuid level //Get max cpuid level
eax = 0x0000000;
cpuid(&eax, &ebx, &ecx, &edx); cpuid(&eax, &ebx, &ecx, &edx);
cpu->maxLevels = eax; cpu->maxLevels = eax;
//Fill vendor //Fill vendor
cpu->cpu_vendor = get_cpu_vendor_internal(eax,ebx,ecx,edx); char name[13];
if(cpu->cpu_vendor == VENDOR_INVALID) { memset(name,0,13);
printf("ERROR: CPU vendor is neither AMD nor INTEL\n"); get_cpu_vendor_internal(name, eax,ebx,ecx,edx);
if(strcmp(VENDOR_INTEL_STRING,name) == 0)
cpu->cpu_vendor = VENDOR_INTEL;
else if (strcmp(VENDOR_AMD_STRING,name) == 0)
cpu->cpu_vendor = VENDOR_AMD;
else {
cpu->cpu_vendor = VENDOR_INVALID;
printErr("Unknown CPU vendor: %s", name);
return NULL; return NULL;
} }
//Get max extended level //Get max extended level
eax = 0x8000000;
cpuid(&eax, &ebx, &ecx, &edx); cpuid(&eax, &ebx, &ecx, &edx);
cpu->maxExtendedLevels = eax; cpu->maxExtendedLevels = eax;
@@ -151,7 +149,7 @@ struct cpuInfo* get_cpu_info() {
} }
} }
else { else {
//We cant afford this check, assume 1 printWarn("Can't read topology information from cpuid (needed level is 0x%.8X, max is 0x%.8X). Assuming HT is disabled", 0x0000000B, cpu->maxLevels);
cpu->HT = 1; cpu->HT = 1;
} }
@@ -172,6 +170,9 @@ struct cpuInfo* get_cpu_info() {
cpu->AVX = (ecx & ((int)1 << 28)) != 0; cpu->AVX = (ecx & ((int)1 << 28)) != 0;
cpu->FMA3 = (ecx & ((int)1 << 12)) != 0; cpu->FMA3 = (ecx & ((int)1 << 12)) != 0;
} }
else {
printWarn("Can't read features information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x00000001, cpu->maxLevels);
}
if (cpu->maxLevels >= 0x00000007){ if (cpu->maxLevels >= 0x00000007){
eax = 0x00000007; eax = 0x00000007;
ecx = 0x00000000; ecx = 0x00000000;
@@ -187,12 +188,18 @@ struct cpuInfo* get_cpu_info() {
((ebx & ((int)1 << 17)) != 0) || ((ebx & ((int)1 << 17)) != 0) ||
((ebx & ((int)1 << 21)) != 0)); ((ebx & ((int)1 << 21)) != 0));
} }
else {
printWarn("Can't read features information from cpuid (needed level is 0x%.8X, max is 0x%.8X)", 0x00000007, cpu->maxLevels);
}
if (cpu->maxExtendedLevels >= 0x80000001){ if (cpu->maxExtendedLevels >= 0x80000001){
eax = 0x80000001; eax = 0x80000001;
cpuid(&eax, &ebx, &ecx, &edx); cpuid(&eax, &ebx, &ecx, &edx);
cpu->SSE4a = (ecx & ((int)1 << 6)) != 0; cpu->SSE4a = (ecx & ((int)1 << 6)) != 0;
cpu->FMA4 = (ecx & ((int)1 << 16)) != 0; cpu->FMA4 = (ecx & ((int)1 << 16)) != 0;
} }
else {
printWarn("Can't read features information from cpuid (needed extended level is 0x%.8X, max is 0x%.8X)", 0x80000001, cpu->maxExtendedLevels);
}
return cpu; return cpu;
} }