···11-// Copyright 2011 Shinichiro Hamaji. All rights reserved.
22-//
33-// Redistribution and use in source and binary forms, with or without
44-// modification, are permitted provided that the following conditions
55-// are met:
66-//
77-// 1. Redistributions of source code must retain the above copyright
88-// notice, this list of conditions and the following disclaimer.
99-//
1010-// 2. Redistributions in binary form must reproduce the above
1111-// copyright notice, this list of conditions and the following
1212-// disclaimer in the documentation and/or other materials
1313-// provided with the distribution.
1414-//
1515-// THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY
1616-// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1717-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1818-// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR
1919-// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2020-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2121-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2222-// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2323-// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2424-// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2525-// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2626-// SUCH DAMAGE.
2727-2828-#ifndef ENV_FLAGS_H_
2929-#define ENV_FLAGS_H_
3030-3131-#include <stdlib.h>
3232-#include <string.h>
3333-3434-#define LD_MAC_EnvToBool(envname, dflt) \
3535- (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL)
3636-3737-#define LD_MAC_DECLARE_VARIABLE(type, name, tn) \
3838- namespace FLAG__namespace_do_not_use_directly_use_LD_MAC_DECLARE_##tn##_instead { \
3939- extern type FLAGS_##name; \
4040- } \
4141- using FLAG__namespace_do_not_use_directly_use_LD_MAC_DECLARE_##tn##_instead::FLAGS_##name
4242-4343-#define LD_MAC_DEFINE_VARIABLE(type, name, value, meaning, tn) \
4444- namespace FLAG__namespace_do_not_use_directly_use_LD_MAC_DECLARE_##tn##_instead { \
4545- type FLAGS_##name(value); \
4646- } \
4747- using FLAG__namespace_do_not_use_directly_use_LD_MAC_DECLARE_##tn##_instead::FLAGS_##name;
4848-4949-#define DECLARE_bool(name) \
5050- LD_MAC_DECLARE_VARIABLE(bool, name, bool)
5151-5252-#define DEFINE_bool(name, value, meaning) \
5353- LD_MAC_DEFINE_VARIABLE(bool, name, LD_MAC_EnvToBool("LD_MAC_" #name, value), \
5454- meaning, bool)
5555-5656-#endif // ENV_FLAGS_H_
···11-// Copyright 2011 Shinichiro Hamaji. All rights reserved.
22-//
33-// Redistribution and use in source and binary forms, with or without
44-// modification, are permitted provided that the following conditions
55-// are met:
66-//
77-// 1. Redistributions of source code must retain the above copyright
88-// notice, this list of conditions and the following disclaimer.
99-//
1010-// 2. Redistributions in binary form must reproduce the above
1111-// copyright notice, this list of conditions and the following
1212-// disclaimer in the documentation and/or other materials
1313-// provided with the distribution.
1414-//
1515-// THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY
1616-// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1717-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1818-// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR
1919-// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2020-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2121-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2222-// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2323-// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2424-// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2525-// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2626-// SUCH DAMAGE.
2727-2828-#include "env_flags.h"
2929-3030-// DEFINE_bool(LOG, false, "Output bunch of logs");
3131-extern "C" bool g_loggingEnabled = false;
3232-3333-__attribute__((constructor)) static void initLogging()
3434-{
3535- const char* v = getenv("DYLD_DEBUG");
3636- if (v && atoi(v))
3737- g_loggingEnabled = true;
3838-}
3939-
···11-// Copyright 2011 Shinichiro Hamaji. All rights reserved.
22-//
33-// Redistribution and use in source and binary forms, with or without
44-// modification, are permitted provided that the following conditions
55-// are met:
66-//
77-// 1. Redistributions of source code must retain the above copyright
88-// notice, this list of conditions and the following disclaimer.
99-//
1010-// 2. Redistributions in binary form must reproduce the above
1111-// copyright notice, this list of conditions and the following
1212-// disclaimer in the documentation and/or other materials
1313-// provided with the distribution.
1414-//
1515-// THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY
1616-// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1717-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1818-// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR
1919-// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2020-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2121-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2222-// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2323-// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2424-// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2525-// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2626-// SUCH DAMAGE.
2727-2828-#ifndef LOG_H_
2929-#define LOG_H_
3030-3131-#include <assert.h>
3232-#include <stdio.h>
3333-3434-#ifdef __cplusplus
3535-# include <iostream>
3636-#endif
3737-3838-//#include "env_flags.h"
3939-4040-//DECLARE_bool(LOG);
4141-extern bool g_loggingEnabled;
4242-#define FLAGS_LOG g_loggingEnabled
11+#ifndef UTIL_LOG_H
22+#define UTIL_LOG_H
4334444-#ifdef NOLOG
4545-# define LOG if (0) std::cout
4646-# define LOGF(...) if (0) fprintf(stderr, __VA_ARGS__)
4747-#else
4848-# define LOG if (FLAGS_LOG) std::cerr
4949-# define LOGF(...) if (FLAGS_LOG) fprintf(stderr, __VA_ARGS__)
5050-#endif
44+#include <cassert>
55+#include <cstdio>
66+#include <iostream>
5175252-#define ERR cerr
88+extern "C" bool g_loggingEnabled;
5395454-#ifdef NDEBUG
5555-// Do an extra check to avoid warning around unused local variables.
5656-# define CHECK(r) do { if (!(r)) assert(r); } while (0)
5757-#else
5858-# define CHECK(r) assert(r);
5959-#endif
1010+#define LOG if (g_loggingEnabled) std::cerr
1111+#define LOGF(...) if (g_loggingEnabled) fprintf(stderr, __VA_ARGS__)
60126113#endif