Skip to content

Commit

Permalink
Fixes to having multiple arguments to logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thebsdbox committed Apr 26, 2018
1 parent eb11b98 commit b39826f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 15 deletions.
15 changes: 11 additions & 4 deletions htmlfiles/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@

<div id="container">
<div id="header">
<h3 style="display: inline;"><span class="blue"> dan@home:~ $</span> find /google/ --name </h3>
<form method="get" action="https://www.google.com/search">
<h3 style="display: inline;"><span class="blue"> dan@home:~ $</span> set_memory_usage </h3>
<form method="get" action="./mem">
<input type="text" autofocus="autofocus" name="q" \>
</form>

</div>
<div id="header">
<h3 style="display: inline;"><span class="blue"> dan@home:~ $</span> set_memory_usage </h3>
<form method="get" action="./cpu">
<input type="text" autofocus="autofocus" name="q" \>
</form>

Expand All @@ -192,8 +199,8 @@ <h3 style="display: inline;"><span class="blue"> dan@home:~ $</span> find /googl

<div class="linksbox">
<h3><span class="blue">dan@home:~\general $</span> ls</h3>
<a class="green" target="blank" href="http://localhost/oom">Out Of Memory</a>
<a class="green" target="blank" href="http://localhost/memlimit">Memory Limit</a>
<a class="green" target="blank" href="./oom">Out Of Memory</a>
<a class="green" target="blank" href="./memlimi">Memory Limit</a>
<a class="green" target="blank" href="https://news.ycombinator.com">"Hacker" News</a>
<a class="green" target="blank" href="https://www.theregister.co.uk">The Register</a>
<a class="green" target="blank" href="http://rarbgunblock.xyz/torrents.php">rargb</a>
Expand Down
3 changes: 2 additions & 1 deletion httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ int receive(int socket)
if ( stringMatch("GET", request->method) ) // GET
{

printf("GET request for %s\n", request->URI);
logInfof("GET request for %s\n", request->URI);

sendHeader("200 OK", contentType, htmlfiles_index_html_len, connecting_socket);
sendString((char *)htmlfiles_index_html,connecting_socket);
if (postData) {
Expand Down
103 changes: 96 additions & 7 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
*/

#include <sys/time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "log.h"

int loglevel;
long seedTime;
#define STDOUT 1

void setLogLevel(int log)
{
Expand All @@ -29,32 +33,117 @@ void logInit(void)
setLogLevel(4);
}

char *convert(unsigned int num, int base)
{
static char Representation[]= "0123456789ABCDEF";
static char buffer[50];
char *ptr;

ptr = &buffer[49];
*ptr = '\0';

do
{
*--ptr = Representation[num%base];
num /= base;
}while(num != 0);

return(ptr);
}

void logDebug(char *text)
{
if (loglevel < 4)
return;
struct timeval tv;
gettimeofday(&tv, NULL);
long newTimeCount = time(NULL);
printf("\x1B[37mDEBG\x1B[0m[%04ld] %s\n", (newTimeCount - seedTime), text);
printf("\x1B[37mDEBG\x1B[0m[%04ld] %s", (newTimeCount - seedTime), text);
}

void logInfo(char *text)
{
if (loglevel < 3)
return;
struct timeval tv;
gettimeofday(&tv, NULL);
long newTimeCount = time(NULL);
printf("\x1B[34mINFO\x1B[0m[%04ld] %s\n", (newTimeCount - seedTime), text);
printf("\x1B[34mINFO\x1B[0m[%04ld] %s", (newTimeCount - seedTime), text);
}

void logInfof(char* format,...)
{
if (loglevel < 3)
return;
char *traverse;
unsigned int i;
char *s;
long newTimeCount = time(NULL);
printf("\x1B[34mINFO\x1B[0m[%04ld] ", (newTimeCount - seedTime));

va_list arg;
va_start(arg, format);
unsigned long stringLength = strlen(format);
for(traverse = format; *traverse != '\0'; traverse++)
{
if (stringLength == 0)
break;
while( *traverse != '%')
{
if (*traverse == '\0') // Check for end of format
break;
putchar(*traverse);
traverse++;
stringLength--;
if (stringLength == 0)
break;
}

if (*traverse == '\0') // If end of format end
break;
traverse++;


switch(*traverse)
{
case 'c' : i = va_arg(arg,int); //Fetch char argument
putchar(i);
break;

case 'd' : i = va_arg(arg,int); //Fetch Decimal/Integer argument
if((signed int)i<0)
{
i = -i;
putchar('-');
}
puts(convert(i,10));
break;

case 'o': i = va_arg(arg,unsigned int); //Fetch Octal representation
puts(convert(i,8));
break;

case 's': s = va_arg(arg,char *); //Fetch string
if (s != NULL) {
fputs(s, stdout);
}
break;

case 'x': i = va_arg(arg,unsigned int); //Fetch Hexadecimal representation
puts(convert(i,16));
break;
}
stringLength--;
}

va_end(arg);
}


void logWarn(char *text)
{
if (loglevel < 2)
return;
long newTimeCount = time(NULL);
printf("\x1B[33mWARN\x1B[0m[%04ld] %s\n", (newTimeCount - seedTime), text);
printf("\x1B[33mWARN\x1B[0m[%04ld] %s", (newTimeCount - seedTime), text);
}

void logError(char *text)
Expand All @@ -64,7 +153,7 @@ void logError(char *text)
struct timeval tv;
gettimeofday(&tv, NULL);
long newTimeCount = time(NULL);
printf("\x1B[31mERRO\x1B[0m[%04ld] %s\n", (newTimeCount - seedTime), text);
printf("\x1B[31mERRO\x1B[0m[%04ld] %s", (newTimeCount - seedTime), text);
}

void logFatal(char *text)
Expand All @@ -74,6 +163,6 @@ void logFatal(char *text)
struct timeval tv;
gettimeofday(&tv, NULL);
long newTimeCount = time(NULL);
printf("\x1B[36mERRO\x1B[0m[%04ld] %s\n", (newTimeCount - seedTime), text);
printf("\x1B[36mERRO\x1B[0m[%04ld] %s", (newTimeCount - seedTime), text);
exit(1);
}
1 change: 1 addition & 0 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void logInit(void); //Needed to enable logging

void logDebug(char *text); //5
void logInfo(char *text); //4
void logInfof(char* format,...); // 4
void logWarn(char *text); //3
void logError(char *text); //2
void logFatal(char *text); //1
Expand Down
3 changes: 1 addition & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main(int argc, char**argv)
break;
case 'w':
// Set a port to bind to
printf("Starting WebServer\n");
logInfo("Starting WebServer\n");
argument = optarg;
int port = atoi(argument);
setPort(port);
Expand All @@ -120,7 +120,6 @@ int main(int argc, char**argv)
break;
}
}

printf("%s\n", getMemoryConfiguration());
//setSignalHander();
while(1) {
Expand Down
2 changes: 2 additions & 0 deletions mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>

#include "utils.h"
#include "log.h"

int urchinMemorySize;
char *urchinMemory;
Expand Down Expand Up @@ -49,6 +50,7 @@ char* getMemoryConfiguration() {
char *oom;
oom = readFile("/proc/sys/vm/overcommit_memory");
if (oom == 0) {
logWarn("Unable to read OOM settings\n");
return "Couldn't Read file [/proc/sys/vm/overcommit_memory]";
}
if (oom[0] == '0' ) {
Expand Down
2 changes: 1 addition & 1 deletion mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

void setMem(int mem);
char* getMemoryConfiguration(void);
int allocateMemory();
int allocateMemory(void);

0 comments on commit b39826f

Please sign in to comment.