all printf calls replaced with kprintf

This commit is contained in:
2022-10-24 18:33:43 +06:00
parent c70544ff97
commit 4785023126
27 changed files with 189 additions and 159 deletions

View File

@@ -24,7 +24,7 @@ void ____Autoarr_free_KVPair_(void* ar){
}
void printkvp(KVPair p){
printf("{\"%s\", ",p.key);
kprintf("{\"%s\", ",p.key);
printuni(p.value);
printf("}");
kprintf("}");
}

View File

@@ -9,6 +9,7 @@ extern "C" {
#include "cptr.h"
#include "optime.h"
#include "type_system/type_system.h"
#include "../kprint/kprintf.h"
#if __cplusplus
}

View File

@@ -1,6 +1,7 @@
#include "std.h"
#include "errors.h"
#include "cptr.h"
#include "../kprint/kprintf.h"
char* errname(ErrorId err){
switch(err){
@@ -47,7 +48,7 @@ void Maybe_free(Maybe e){
}
void printMaybe(Maybe e){
if(e.errmsg) printf("%s\n",e.errmsg);
if(e.errmsg) kprintf("%s\n",e.errmsg);
else printuni(e.value);
}

View File

@@ -36,7 +36,7 @@ void printMaybe(Maybe e);
#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull}
#define __EXIT(ERRMSG) ({ printf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
#define __EXIT(ERRMSG) ({ kprintf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
char* __doNothing(char* a);
char* __unknownErr( );

View File

@@ -11,7 +11,7 @@
(codeblock);\
clock_gettime(CLOCK_REALTIME, &stop);\
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
})
#else // standard low precision clock
#define optime(opname,repeats,codeblock) ({\
@@ -20,6 +20,6 @@
(codeblock);\
clock_t stop=clock();\
double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
})
#endif

View File

@@ -24,7 +24,7 @@ typedef uint64_t uint64;
typedef float float32;
typedef double float64;
#define dbg(N) printf("\e[95m%d\n",N)
#define dbg(N) kprintf("\e[95m%d\n",N)
#ifdef _MSC_VER
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK

View File

@@ -1,5 +1,5 @@
#include "base_toString.h"
#include "../cptr.h"
#include "../base.h"
#include "../../kprint/kprint_format.h"
char* __toString_char(void* c, uint32 fmt) {
@@ -133,7 +133,7 @@ char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){
case kprint_fmtHex:\
return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\
default:\
printf("\n%u\n", kprint_format_dataFormat(f));\
kprintf("\n%u\n", kprint_format_dataFormat(f));\
throw(ERR_FORMAT);\
return NULL;\
}\
@@ -153,7 +153,7 @@ __toString_int_def(64)
case kprint_fmtHex:\
return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\
default:\
printf("\n%u\n", kprint_format_dataFormat(f));\
kprintf("\n%u\n", kprint_format_dataFormat(f));\
throw(ERR_FORMAT);\
return NULL;\
}\
@@ -173,7 +173,7 @@ __toString_uint_def(64)
case kprint_fmtHex:\
return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\
default:\
printf("\n%u\n", kprint_format_dataFormat(f));\
kprintf("\n%u\n", kprint_format_dataFormat(f));\
throw(ERR_FORMAT);\
return NULL;\
}\

View File

@@ -28,8 +28,8 @@ char* __toString_float32(void* n, uint32 fmt);
char* __toString_float64(void* n, uint32 fmt);
// universal functions
char* toString_bin(char* bytes, uint32 size, bool withPrefix);
char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase);
char* toString_bin(void* bytes, uint32 size, bool withPrefix);
char* toString_hex(void* bytes, uint32 size, bool withPrefix, bool uppercase);
#if __cplusplus
}

View File

@@ -46,7 +46,7 @@ typedef enum{
ktDescriptorsState initState=NotInitialized;
void ktDescriptors_beginInit(){
printf("\e[94mtype descriptors initializing...\n");
kprintf("\e[94mtype descriptors initializing...\n");
__ktDescriptors=Autoarr_create(ktDescriptor, 256, 256);
if(__ktDescriptors==NULL) throw(ERR_NULLPTR);
}
@@ -55,7 +55,7 @@ void ktDescriptors_endInit(){
typeDescriptors=Autoarr_toArray(__ktDescriptors);
Autoarr_free(__ktDescriptors,true);
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
printf("\e[92minitialized %u type descriptors\n", ktId_last);
kprintf("\e[92minitialized %u type descriptors\n", ktId_last);
}
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32)){
@@ -71,7 +71,7 @@ void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*t
ktDescriptor ktDescriptor_get(ktId id){
if(id>ktId_last) {
printf("\ntype id: %u\n",id);
kprintf("\ntype id: %u\n",id);
throw("invalid type id");
}
return typeDescriptors[id];

View File

@@ -1,3 +1,6 @@
# kprintf
It is just my variant of printf.
# kprint
I don't really like printf function (and its variants), so i made safer and more convinient replacement.

View File

@@ -1,6 +1,5 @@
#include "../String/StringBuilder.h"
#include "kprint.h"
#include "../base/type_system/base_toString.h"
ktId __typeFromFormat(kprint_format f){
ktId typeId=kprint_format_ktId(f);
@@ -78,7 +77,7 @@ void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects){
#define FOREGROUND_YELLOW FOREGROUND_GREEN | FOREGROUND_RED
DWORD kprint_fgColor_toWin(kprint_fgColor f){
//printf("fg: %x\n", f);
//kprintf("fg: %x\n", f);
switch(f){
case kprint_fgBlack: return 0;
case kprint_fgDarkRed: return FOREGROUND_RED;
@@ -101,7 +100,7 @@ DWORD kprint_fgColor_toWin(kprint_fgColor f){
}
DWORD kprint_bgColor_toWin(kprint_bgColor f){
//printf("bg: %x\n", f);
//kprintf("bg: %x\n", f);
switch(f){
case kprint_bgBlack: return 0;
case kprint_bgDarkRed: return BACKGROUND_RED;
@@ -140,13 +139,13 @@ void kprint_setColor(kprint_format f){
uint8 fg=(f&0x0f000000)>>24;
if(fg<8) fg+=30;
else fg+=90-8;
printf("\e[%um", fg);
kprintf("\e[%um", fg);
}
if(kprint_format_bgColorChanged(f)){
uint8 bg=(f&0x00f00000)>>20;
if(bg<8) bg+=40;
else bg+=100-8;
printf("\e[%um", bg);
kprintf("\e[%um", bg);
}
}
#endif
@@ -167,59 +166,3 @@ void kprint_setColor(kprint_format f){
StringBuilder_append_char(strb, ' ');
StringBuilder_append_char(strb, ']');
} */
void kprintf(char* format, ...){
va_list vl;
va_start(vl, format);
char c;
while((c=*format++)){
if(c=='%'){
c=*format++;
switch (c) {
case 'u':
toString_uint(va_arg(vl, uint64),0,0);
break;
case 'i':
toString_int(va_arg(vl, uint64));
break;
case 'f':
toString_float(va_arg(vl, float64),0,0);
break;
/* case 'l':
c=*format++;
switch (c) {
case 'u':
toString_uint(va_arg(vl, uint64),0,0);
break;
case 'i':
toString_int(va_arg(vl, uint64));
break;
case 'f':
toString_float(va_arg(vl, float64),0,0);
break;
default:
throw(ERR_FORMAT);
}
break; */
case 'p':
case 'x':
uint64 px=va_arg(vl, uint64);
toString_hex(&px,sizeof(px),1,0);
break;
default:
throw(ERR_FORMAT);
}
} else if(c=='\e'){
IFWIN(
({
}),
putc(c,stdout);
)
} else {
putc(c,stdout);
}
}
va_end(vl);
}

View File

@@ -65,9 +65,6 @@ Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, __kprint_value_union*
void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects);
#define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes))
// cross-platform printf analog
void kprintf(char* format, ...);
// can take (bgColor | fgColor)
void kprint_setColor(kprint_format f);

73
src/kprint/kprintf.c Normal file
View File

@@ -0,0 +1,73 @@
#include "kprintf.h"
#include "../base/base.h"
#include "../base/type_system/base_toString.h"
void kprintf(const char* format, ...){
va_list vl;
va_start(vl, format);
char c;
while((c=*format++)){
if(c=='%'){
char* argstr=NULL;
c=*format++;
format_escape_seq:
switch (c) {
case 'u':
argstr=toString_uint(va_arg(vl, uint64),0,0);
break;
case 'i':
argstr=toString_int(va_arg(vl, uint64));
break;
case 'f':
argstr=toString_float(va_arg(vl, float64),0,0);
break;
case 'l':
c=*format++;
goto format_escape_seq;
// switch (c) {
// case 'u':
// argstr=toString_uint(va_arg(vl, uint64),0,0);
// break;
// case 'i':
// argstr=toString_int(va_arg(vl, uint64));
// break;
// case 'f':
// argstr=toString_float(va_arg(vl, float64),0,0);
// break;
// default:
// throw(ERR_FORMAT);
// }
// break;
case 'p':
case 'x':
uint64 px=va_arg(vl, uint64);
argstr=toString_hex(&px,sizeof(px),1,0);
break;
case 's':
fputs(va_arg(vl,char*), stdout);
break;
case 'c':
argstr=malloc(2);
argstr[0]=va_arg(vl,char);
argstr[1]=0;
break;
default:
throw(ERR_FORMAT);
}
if(argstr){
fputs(argstr, stdout);
free(argstr);
}
} else if(c=='\e'){
IFWIN(
({
}),
putc(c,stdout);
)
} else {
putc(c,stdout);
}
}
va_end(vl);
}

12
src/kprint/kprintf.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
// cross-platform kprintf analog
void kprintf(const char* format, ...);
#if __cplusplus
}
#endif