Log.inl

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @file   Log.inl
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @author Matthew Jacobs (jacobs.mh@gmail.com)
00005    @date   Fall 2008
00006 
00007    @brief
00008       Unified logging functionality for Milton debugging output
00009    <!-------------------------------------------------------------------->**/
00010 
00011 #ifndef LOG_INL_
00012 #define LOG_INL_
00013 
00014 #include <execinfo.h>
00015 
00016 #define TAB_LENGTH_ (2)
00017 
00018 inline std::ostream &operator<<(Log &log, const std::string &s) {
00019    if (log._debug) {
00020       const int depth = MIN(255, MAX(0, log._getDepth() - log._depth));
00021       const std::string tab(depth * TAB_LENGTH_, ' ');
00022       
00023       // automatic indentation dependent on how deep the calling function 
00024       // is in the stack :)
00025       return (log.info << tab << s);
00026    }
00027    
00028    // restrict output with null ostream
00029    return Log::s_null;
00030 }
00031 
00032 inline std::ostream &operator<<(Log &log, const char *s) {
00033    if (log._debug) {
00034       const int curDepth = log._getDepth();
00035       const int depth = MIN(255, MAX(0, curDepth - log._depth));
00036       const std::string tab(depth * TAB_LENGTH_, ' ');
00037       
00038       // automatic indentation dependent on how deep the calling function 
00039       // is in the stack :)
00040       return (log.info << tab << s);
00041    }
00042    
00043    // restrict output with null ostream
00044    return Log::s_null;
00045 }
00046 
00047 inline int Log::_getDepth() const {
00048    const size_t max_depth = 256;
00049    size_t stack_depth;
00050    void *stack_addrs[max_depth];
00051    
00052    stack_depth = backtrace(stack_addrs, max_depth) - 1;
00053    ASSERT(stack_depth >= 0);
00054    
00055    return stack_depth;
00056 }
00057    
00058 /*void print_trace(FILE *out, const char *file, int line) {
00059    const size_t max_depth = 100;
00060    size_t stack_depth;
00061    void *stack_addrs[max_depth];
00062    char **stack_strings;
00063 
00064    stack_depth = backtrace(stack_addrs, max_depth);
00065    stack_strings = backtrace_symbols(stack_addrs, stack_depth);
00066 
00067    fprintf(out, "Call stack from %s:%d:\n", file, line);
00068 
00069    for (size_t i = 1; i < stack_depth; i++) {
00070       fprintf(out, "    %s\n", stack_strings[i]);
00071    }
00072    free(stack_strings); // malloc()ed by backtrace_symbols
00073    fflush(out);
00074 }*/
00075 
00076 #undef TAB_LENGTH_
00077 
00078 #endif // LOG_INL_
00079 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6