Skip to content

Instantly share code, notes, and snippets.

@krshock
Last active December 25, 2018 15:17
Show Gist options
  • Save krshock/9c24429c31663179d60dc52228ec80e8 to your computer and use it in GitHub Desktop.
Save krshock/9c24429c31663179d60dc52228ec80e8 to your computer and use it in GitHub Desktop.
A basic @nogc alternative to writeln for Dlang
module logger;
/*
- Supported types: string, int, float, bool
- License CC0, URL: https://creativecommons.org/publicdomain/zero/1.0/
Using logger.d:
-------------------
import logger;
@nogc
void helloWorld()
{
log("Hello World");
log(0);
log(true);
log(10, "bottles of rum");
int message_count = 10;
log("You have", message_count, "messages");
}
*/
@nogc
void log(A...)(A a)
{
bool first=true;
foreach(t; a){
if(!first){
logger(" ");
}else{
first=false;
}
logger(t);
}
logger("\n");
}
@nogc
private void logger(string st){
import core.stdc.stdio;
fprintf(stdout, "%s", st.ptr);
}
@nogc
private void logger(int i){
import core.stdc.stdio;
fprintf(stdout, "%i", i);
}
@nogc
private void logger(float f){
import core.stdc.stdio;
fprintf(stdout, "%f", f);
}
@nogc
private void logger(bool b){
import core.stdc.stdio;
if(b)
fprintf(stdout, "true");
else
fprintf(stdout, "false");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment