DebStream.cc 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
// (C) Copyright 2014 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk,  Inc.,  and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of  this information  by  anyone  other  than  authorized
// employees of Autodesk, Inc.  is granted  only under a written
// non-disclosure agreement,  expressly  prescribing  the  scope
// and manner of such use.

#include "DebOut.hh"
#include <string>
#include <fstream>
#include <time.h>
#include <vector>
#include <iostream>

#ifdef DEB_out_INCLUDED

#define LOCAL_PROC static

LOCAL_PROC bool is_html_filename(const char * const str)  
{
  const char * dot = strchr(str, '.');
  if (dot == nullptr) return false;
  ++dot;
  return ( !strncmp(dot, "htm", 3) ) || ( !strncmp(dot,"HTM",3) ) ;
}

namespace ReForm
{

// This is currently in DebOut.hh instead
#if 0
  class DebStream
  {
  public:
    enum StreamType
    {
      append    = 0x01,
      html      = 0x02,
      retain    = 0x04,
      keep_open = 0x08
    };

  private:
    class Impl;
    Impl * impl_;
  public:
    //! Constructor.
    DebStream(
      const char * _file_name = nullptr,  //!< [in] File name if file based. 
      const StreamType _type = append    //!< [in] bitsfield enum type identifier
      ) ;
    ~DebStream();

    DebStream& print(const int);
    DebStream& print(const double);
    DebStream& print(const char * const);
    DebStream& print(const char);

    //! Output to file, if any.
    int flush();

    //! Add footer, flush, and close file.
    void close();

    //! Wipe all buffers, rendering future flush() inactive.
    void clear();

    //! Set debug level.
    void set_level(int, int _warn=0);

    //! Are we at the start of a text line?
    bool at_line_start();

    //! Get the currently active DebStream
    static DebStream& get_global(int _warn=0);

    static void set_global_name(const char * name, int _warn = 0);
    void set_file_name(const char * _name);

    bool permission(int _lev, int _warn=0);

    void indent(int _depth);

    //void add_to_call_stack(const char * _funcname, const int _count);
    class CallStack;
    class FunctionCallSequence;
    CallStack& call_stack();

  private:
    Impl * impl() { return impl_;}

  };
#endif


  // Can't easily put these in impl anymore
  
  class DebStream::FunctionCallSequence
  {
    std::string func_name_;
    std::vector<int> counts_; // These may not be sequential when multithreaded.

  public:
    DebStream::FunctionCallSequence(const char * _func_name, const int _count) 
      : func_name_(_func_name)
    {
      counts_.push_back(_count);
    }
    ~FunctionCallSequence() {}

    bool add(const char * _func_name, const int _count)
    {
      if (func_name_ == _func_name)
      {
        counts_.push_back(_count);
        return true;
      }
      return false;
    }

    bool pop()
    {
      if (counts_.size() > 1 )
      {
        counts_.pop_back();
        return true;
      }
      counts_.clear();
      return false;
    }

    int number_calls() const 
    {
      if (!this) return 0;
      return (int)counts_.size();
    }

    int count(int i=0) const
    { 
      int num = number_calls();
      if (i < num) return counts_[num -1 - i];
      return -1;
    }

    const char * name() const { return func_name_.c_str();}

    // Replace interior of < > in function name with . for brevity
    void compact_name(std::string & str) const
    {
      int cnt=0;
      const char * ptr = func_name_.c_str();
      while (ptr && (*ptr != '\0'))
      {
        char c = *ptr;
        if (c=='>') --cnt;
        if (cnt==0) str.append(1, c);
        if (c=='<') 
        {
          if (cnt==0) str.append(".");
          ++cnt;
        }
        ++ptr;
      }
    }

    // Get single call stack element string
    void get(std::string& str, const bool _strip_angled, bool _with_counts) const
    {
      if (_strip_angled) compact_name(str);
      else str.append(name());
      str.append("[");
      if (_with_counts)
      {

        int num = number_calls();
        int prev = -2;
        int seq_cnt =0;
        for (int i=0; i < num; ++i)
        {
          int cnt = counts_[i];
          if (cnt != prev + 1)
          {
            char buffer[64];

            if (seq_cnt > 0)
            {
              str.append("-");
              sprintf_s(buffer, sizeof(buffer), "%i", prev);
              str.append(buffer);
            }
            if (i>0) str.append(",");
            sprintf_s(buffer, sizeof(buffer), "%i", cnt);
            str.append(buffer);
            seq_cnt=0;
          }
          else
            ++seq_cnt;
          prev = cnt;
        } // endfor i
      } // endif _with_counts
      else
      {
        str.append("*");
      }
      str.append("]");
    } // endfunc get

  }; // endclass FunctionCallSequence


  class DebStream::CallStack
  {
    std::vector<FunctionCallSequence> calls_;
    int depth_;
  public:
    CallStack() : depth_(0) {}
    ~CallStack() {}

    void add(const char * _func_name, const int _count)
    {
      if (calls_.empty() || !calls_.back().add(_func_name, _count))
      {
        calls_.push_back( FunctionCallSequence(_func_name, _count) );
      }
      ++depth_;
    }

    void pop()
    {
      if (!calls_.back().pop())
        calls_.pop_back();
      --depth_;
    }

    const FunctionCallSequence * call( int _up =0)
    {
      int num = (int)calls_.size();
      if (_up < num) return &calls_[num - 1 - _up];
      return nullptr;
    }

    // Read a particular call stack element
    bool read(int _up, const char*& _funcname, int & _count)
    {
      const FunctionCallSequence * fcs = call(_up);
      if (fcs != nullptr)
      {
        _funcname = fcs->name();
        _count = fcs->count(0); // Return most recent deb_enter_count
        return true;
      }
      return false;
    }

    // Get a full call stack sting.
    // returns number of call stack function elements 
    int get(std::string& _str, bool _with_counts = true) const
    {
      int num = (int)calls_.size();
      for (int i =0; i< num; ++i)
      {
        if (i>0) _str.append("->");
        calls_[i].get(_str, true, _with_counts);
      }
      return num;
    }

    int depth() const { return depth_; }

  }; // endclass CallStack
 


class DebStream::Impl
{
private:
  StreamType type_;
  int lev_;
  int num_flush_;
  std::string current_;
  std::string output_;
  std::string file_name_;
  std::fstream file_stream_;
  std::string indent_string_;
  DebStream * deb_stream_;
  CallStack call_stack_;

public:

  CallStack& call_stack() { return call_stack_;}

295 296 297 298
  bool is_kept_open() const  { return 0 != (type_ & StreamType::keep_open); }
  bool is_html() const  { return 0 != (type_ & StreamType::html); }
  bool is_retained() const  { return 0 != (type_ & StreamType::retain); }
  bool is_appended() const  { return 0 != (type_ & StreamType::append); }
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  // Only applies to HTML DEB_out
  bool is_white_on_black() { return true; }

  bool file_is_open() const { return file_stream_.is_open(); }

  bool fork_to_cout() { return false; }
  bool fork_to_cerr() { return true; }


  const char * file_name() const
  {
   if ( this && (!file_name_.empty()) ) return file_name_.c_str();
    return nullptr;
  }
  
  void clear()
  {
    current_.clear();
    output_.clear();
    file_name_.clear();
  }

  Impl(
    DebStream* _deb_stream,
    StreamType _type = (StreamType) (append | retain), 
    const char * _file_name = nullptr) :
325
    type_(_type), lev_(5), deb_stream_(_deb_stream), num_flush_(0)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  {
    set_file_name(_file_name);
    indent_string_ = ".";
  }

  ~Impl() {}

  char prev_char() const
  {
    if (!current_.empty()) return current_.back();
    if (!output_.empty()) return output_.back();
    return '\0';
  }

  void line_break(bool _with_indent=false)
  {
    _with_indent;
    if ( is_html() ) current_.append("<br>"); // Don't bother with matching </br>
    current_.append("\n", 1);
    // Don't add indent yet because we might not want it.
  }

  void indent(int _depth)
  {
    if (!indent_string_.empty())
    {
      for (int i=0; i < _depth; ++i) current_.append(indent_string_);
    }
  }
  
  void set_level(int _lev) { lev_ = _lev; }

  void print(const char _c)
  {
    if (_c == '\n') line_break();
    else current_.append(&_c, 1);
  }

  void print(const char * const _s, bool _fork = true)
  {
    if (_s != nullptr)
    {
      for (int i=0; ;++i)
      {
        const char c = _s[i];
        if(c=='\0') break;
        print(c);
      }
      if (_fork)
      {
        if (fork_to_cout()) std::cout << _s;
        if (fork_to_cerr()) std::cerr << _s;
      }

    } 
  }

  void print(int _i)
  {
    char buffer[64];
    sprintf_s(buffer, sizeof(buffer), "%i", _i);
    print(buffer);
  }
  
  void print(double _d)
  {
    char buffer[64];
    sprintf_s(buffer, sizeof(buffer), "%.17g", _d);
    print(buffer);
  }

  DebStream & stream() { return *deb_stream_; }
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

  // Append current asctime to given string 
  bool add_time(std::string & str)
  {
    time_t rawtime;
    time ( &rawtime );
    struct tm timeinfo;
    errno_t err = localtime_s( &timeinfo, &rawtime );
    if (err == 0)
    {
      char buffer[256];
      err = asctime_s(buffer, sizeof(buffer), &timeinfo);
      if (err == 0)
      {
        str.append( buffer );
        return true;
      }
    }
    return false;
  }

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  void header(std::string & str)
  { 
    if ( is_html() )
    {
      str.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">");
      str.append("\n<HTML><HEAD>");
      str.append("\n<TITLE>ReForm DEB_out");
      str.append("</TITLE>");
      // javascript lib loads go here
      // stylesheet loads go here
      // within HEAD javascript goes here
      str.append("\n</HEAD>");
      if( is_white_on_black() )
      {
        str.append( "\n<BODY BGCOLOR=\"#000000\" TEXT=\"#FFFFFF\" LINK=\"#%00FFFF\" VLINK=\"#FFFF00\" >");
      }
      else
      {
        str.append("\n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#%FF0000\" VLINK=\"#0000FF\" >");
      }
      str.append("\n");
    } // endif is_html
    bool date_header = true;
    if (date_header)
    {
444
      add_time(str);
445 446 447 448 449 450 451 452 453
      str.append( "[ Build: " __TIME__  " " __DATE__  "] \n");
    }
  }

  void footer()
  {
    bool date_footer = true;
    if (date_footer)
    {
454 455 456
      std::string str = " \n Closed: ";
      add_time(str);
      stream() << str << "\n";
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    }

    if (is_html())
    {
      stream() << "\n</BODY></HTML>";
    }

  }
  bool is_first_flush()
  {
    return num_flush_ == 0 ;
  }
  
  int flush()
  {
    int res = 0;
    if ( !current_.empty() )
    {
      const char * fname = file_name();
      if((fname != nullptr) || file_is_open() )
      {
        if ( !file_is_open() )
        {
          file_stream_.open( fname, 
            std::fstream::out | ( (is_appended() && !is_first_flush() ) ? 
                std::fstream::app  :  std::fstream::trunc ) );
        }

        if (file_stream_.is_open())
        {
          std::string hdr;
          if ( !is_appended() )
          { // Reoutput entire file
            header(hdr);
            output_.append(hdr);
            file_stream_ << output_;
          }
          else
          {
            if ( is_first_flush() )
            {
              header(hdr);
              if ( is_retained() )
                output_.append( hdr);
              file_stream_ << hdr;
            }
            ++num_flush_;
          }
          file_stream_ << current_;

          if ( is_retained() )
            output_.append(current_);

          current_.clear(); 
          if ( !is_kept_open() )
            file_stream_.close();
        } // endif fs.is_open
        else
        {
          res = -1;
        }
      }
    } // endif current empty
  return res;
  }  // endfunc flush

  void close()
  {
    footer();
    flush();
  }

  bool permission(int _lev)
  {
    return _lev <= lev_;
  }

  void set_file_name(const char * _name)
  {
    file_name_ = _name;
    if ( is_html_filename(_name) )
      type_ = (StreamType)(type_ | StreamType::html);

  }
}; // endclass DebStream::Impl

// =====================================
//      DebEnter member funcs
// =====================================

DebEnter::DebEnter(const char * _funcname, int _count) 
{
  deb_outs_ = 0;
  stream(0, false).call_stack().add(_funcname, _count);
}

DebEnter::~DebEnter() 
{
  stream(0, false).call_stack().pop();
}

DebStream& DebEnter::stream(int _warn, bool _print)
{
  DebStream & ds = ReForm::DebStream::get_global(_warn);
  
  if( _print)
  {
    if ( deb_outs_ < 1)
    {
      // First DEB_out in this function so output callstack, and flush.
      std::string str("***");
      ds.call_stack().get(str);
      str.append("\n");
      ds.print(str.c_str(), false); // Don't fork callstack to cerr etc.
      ds.flush();
    }
    else
    {
      if ( ds.at_line_start() )
      {
        ds.indent(  ds.call_stack().depth() );
      }
    }
    ++deb_outs_;
  }

  return ds;
}

bool DebEnter::permission(int _lev, int _warn)
{ 
  return ReForm::DebStream::get_global().permission(_lev, _warn);
}

// =====================================
//        DebStream member funcs
// =====================================


int DebStream::flush()
{
  return impl_->flush();
}

void DebStream::close()
{
  impl_->close();
}


DebStream::DebStream(const char * _file_name, StreamType _type)
{
  // NB. disable DEB_out over this new if new contains DEB_out
  impl_ = new Impl(this, _type, _file_name);
}

DebStream::~DebStream()
{
  if ( impl_ != nullptr)
  {
    close();
    clear();
    // NB. disable DEB_out over this delete if delete contains DEB_out
    delete impl_;
    impl_ = nullptr;
  }
}

void DebStream::clear()
{
  impl_->clear();
}

DebStream& DebStream::print(const int _i)
{
  impl_->print(_i);
  return *this;
};

DebStream& DebStream::print(const double _d)
{
  impl_->print(_d);
  return *this;
};

DebStream& DebStream::print(const char * _s, bool _fork)
{
  impl_->print(_s, _fork);
  return *this;
};

DebStream& DebStream::print(const char _c)
{
  impl_->print(_c);
  return *this;
};

DebStream & DebStream::get_global(int _warn)
{
  _warn; 
  // TODO: Replace with a Singleton?? ThreadArray??
  //static DebStream g_ds__("reform_deb_out.htm");
  static DebStream g_ds__("reform_deb_out.txt");
  return g_ds__;
}

void DebStream::set_global_name(const char * _name, int _warn)
{
  get_global(_warn).set_file_name(_name);
}

bool DebStream::permission(int _lev, int _warn)
{
  if (this)
  {
    _warn; // ignore for now
    return impl()->permission(_lev);
  }
  return false;
}

void DebStream::set_level(int _lev, int _warn)
{
  _warn; // ignore for now
  if (this) impl()->set_level(_lev);
}

void DebStream::indent(int _i)
{
  impl()->indent(_i);
}

DebStream::CallStack& DebStream::call_stack() 
{
  return impl()->call_stack();
}

void DebStream::set_file_name(const char * _name)
{
  impl()->set_file_name(_name);
}

bool DebStream::at_line_start() 
{
  char prev = impl()->prev_char();
  return (prev=='\n') || (prev=='\0');
}

DebStream& operator<<(DebStream& ds, const int i ) { return ds.print(i);}
DebStream& operator<<(DebStream& ds, const double d ) { return ds.print(d);}
DebStream& operator<<(DebStream& ds, const char * const s ) { return ds.print(s);}
DebStream& operator<<(DebStream& ds, const char c) { return ds.print(c);}

DebStream& operator<<(DebStream& ds, const size_t i ) { return ds.print((int)i);}
DebStream& operator<<(DebStream& ds, const unsigned int i ) { return ds.print((int)i);}
DebStream& operator<<(DebStream& ds, const float f ) { return ds.print((double)f);}

DebStream& operator<<(DebStream& ds, const std::string& s ) { return ds.print(s.c_str());}

}//namespace


#endif // DEB_out_INCLUDED