Skip to content

Commit

Permalink
Merge branch 'feature/time'
Browse files Browse the repository at this point in the history
  • Loading branch information
vzaccaria committed Jan 15, 2015
2 parents cfa97ac + 25e565a commit 1c4e603
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 20 deletions.
35 changes: 35 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,41 @@ then:
test/test3.cpp:10 this is main
```

## Time output

To enable time measurements between debug messages associated with
the same namespace (or the same line of code when using `debugm`), use `DEBUG_TIME=yes`:

```c++
/* example.cpp */
#include <iostream>
#include "debug.hxx"
#include <unistd.h>

using namespace std;

int main(int argc, char const *argv[])
{
for(int i=0; i<4; i++) {
debugm("debug printed.");
sleep(1);
}
return 0;
}
```
Run it with:
```
> DEBUG_COLORS=no DEBUG=* DEBUG_TIME=yes $srcdir/../bin/test6
test/test6.cpp:10 debug printed. +0ms
test/test6.cpp:10 debug printed. +1002ms
test/test6.cpp:10 debug printed. +2003ms
test/test6.cpp:10 debug printed. +3005ms
```
## Conventions
Expand Down
75 changes: 63 additions & 12 deletions debug.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ THE SOFTWARE.
#include <regex>
#include <functional>
#include <vector>
#include <ctime>
#include <iomanip>
#include <chrono>

static std::vector<std::string> words(std::string str, std::string delim = " ") {
std::vector<std::string> res;
Expand Down Expand Up @@ -58,20 +61,39 @@ static auto Debug = [](std::string moduleName) {

auto h = 0;
auto debug_colors = std::getenv("DEBUG_COLORS");
auto debug_time = std::getenv("DEBUG_TIME");

bool use_colors;
bool use_time;

auto name_to_be_hashed = moduleName;

if (debug_colors == NULL || (
std::string(debug_colors) != "false" &&
std::string(debug_colors) != "no" &&
std::string(debug_colors) != "disabled")) {
use_colors = true;
}
else {
use_colors = false;
}
auto getBool = [](const char *e, bool def) -> bool {
if(def == true) {
if (e == NULL || (
std::string(e) != "false" &&
std::string(e) != "no" &&
std::string(e) != "disabled")) {
return true;
}
else {
return false;
}
} else {
if(e != NULL && (
std::string(e) == "true" ||
std::string(e) == "yes" ||
std::string(e) == "enabled")) {
return true;
} else {
return false;
}
}
};

use_colors = getBool(debug_colors, true);
use_time = getBool(debug_time, false);


if(use_colors) {
std::hash<std::string> hash_fn;
Expand Down Expand Up @@ -100,13 +122,42 @@ static auto Debug = [](std::string moduleName) {
shouldDebug = std::regex_search(moduleName, re);
}
return [=](std::string message) {

std::string time_suffix = "";
int diff = 0;

if(use_time) {
auto get_ms = []() -> int {
using namespace std::chrono;
return duration_cast< milliseconds >(high_resolution_clock::now().time_since_epoch()).count();
};
static auto prev = get_ms();
auto curr = get_ms();
diff = curr - prev;
prev = curr;
}

#define _col_light(c, m) ("\u001b[9" + std::to_string(c) + "m" + m + " \u001b[0m" )
#define _col_light_ms(c, m) ("\u001b[9" + std::to_string(c) + "m" + m + "ms \u001b[0m" )


if (!shouldDebug) { return false; }

if (!use_colors) {
std::cerr << " " << moduleName;
std::cerr << " " << message << std::endl;
std::cerr << " " << message;
} else {
std::cerr << _col_light(colors[h], moduleName);
std::cerr << message;
}
if(use_time) {
if(use_colors) {
std::cerr << " +" << _col_light_ms(colors[h], std::to_string((diff))) << std::endl;
} else {
std::cerr << " +" << std::to_string((diff)) << "ms" << std::endl;
}
} else {
std::cerr << " \u001b[9" << colors[h] << "m" << moduleName;
std::cerr << " \u001b[0m" << message << "\u001b[3" << h << "m" << std::endl;
std::cerr << std::endl;
}
return true;
};
Expand Down
6 changes: 6 additions & 0 deletions makefile.ls
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ parse ->
@clang 'deps/*/**.cpp', 'deps/*/**.{hxx}'
@clang 'test/test5.cpp', '*.hxx'
]

@dest "./bin/test6", ->
@link -> [
@clang 'deps/*/**.cpp', 'deps/*/**.{hxx}'
@clang 'test/test6.cpp', '*.hxx'
]
]

@collect "all", ->
Expand Down
2 changes: 1 addition & 1 deletion test/out1c.ref
Original file line number Diff line number Diff line change
@@ -1 +1 @@
main this is main
main this is main
4 changes: 4 additions & 0 deletions test/out6.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test/test6.cpp:10 debug printed. +0ms
test/test6.cpp:10 debug printed. +1000ms
test/test6.cpp:10 debug printed. +1000ms
test/test6.cpp:10 debug printed. +1000ms
4 changes: 4 additions & 0 deletions test/out6.tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test/test6.cpp:10 debug printed. +0ms
test/test6.cpp:10 debug printed. +1000ms
test/test6.cpp:10 debug printed. +1000ms
test/test6.cpp:10 debug printed. +1000ms
4 changes: 4 additions & 0 deletions test/out6.tmp.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test/test6.cpp:10 debug printed. +0ms
test/test6.cpp:10 debug printed. +1001ms
test/test6.cpp:10 debug printed. +1001ms
test/test6.cpp:10 debug printed. +1001ms
18 changes: 11 additions & 7 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ srcdir=`dirname $0`
srcdir=`cd $srcdir; pwd`
dstdir=`pwd`

DEBUG_COLORS=no $srcdir/../bin/test &> $srcdir/out0.tmp
DEBUG_COLORS=no DEBUG=main $srcdir/../bin/test &> $srcdir/out1.tmp
DEBUG=main $srcdir/../bin/test &> $srcdir/out1c.tmp
DEBUG_COLORS=no DEBUG=* $srcdir/../bin/test &> $srcdir/out2.tmp
DEBUG_COLORS=no DEBUG=* $srcdir/../bin/test2 &> $srcdir/out3.tmp
DEBUG_COLORS=no $srcdir/../bin/test4 &> $srcdir/out4.tmp
DEBUG_COLORS=no DEBUG=main* $srcdir/../bin/test5 &> $srcdir/out5.tmp
DEBUG_COLORS=no $srcdir/../bin/test &> $srcdir/out0.tmp
DEBUG_COLORS=no DEBUG=main $srcdir/../bin/test &> $srcdir/out1.tmp
DEBUG=main $srcdir/../bin/test &> $srcdir/out1c.tmp
DEBUG_COLORS=no DEBUG=* $srcdir/../bin/test &> $srcdir/out2.tmp
DEBUG_COLORS=no DEBUG=* $srcdir/../bin/test2 &> $srcdir/out3.tmp
DEBUG_COLORS=no $srcdir/../bin/test4 &> $srcdir/out4.tmp
DEBUG_COLORS=no DEBUG=main* $srcdir/../bin/test5 &> $srcdir/out5.tmp

DEBUG_COLORS=no DEBUG=* DEBUG_TIME=yes $srcdir/../bin/test6 &> $srcdir/out6.tmp.t
cat $srcdir/out6.tmp.t | sed 's/.ms/0ms/g' > $srcdir/out6.tmp

function check {
m=$1
Expand All @@ -30,5 +33,6 @@ check '4' $srcdir/out3.tmp $srcdir/out3.ref
check '5' $srcdir/out1c.tmp $srcdir/out1c.ref
check '6' $srcdir/out4.tmp $srcdir/out4.ref
check '7' $srcdir/out5.tmp $srcdir/out5.ref
check '8' $srcdir/out6.tmp $srcdir/out6.ref


14 changes: 14 additions & 0 deletions test/test6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include "debug.hxx"
#include <unistd.h>

using namespace std;

int main(int argc, char const *argv[])
{
for(int i=0; i<4; i++) {
debugm("debug printed.");
sleep(1);
}
return 0;
}

0 comments on commit 1c4e603

Please sign in to comment.