DebugStreamFull.C 22.6 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2015, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
brugger's avatar
 
brugger committed
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
brugger's avatar
 
brugger committed
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
brugger's avatar
 
brugger committed
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

39
#include <DebugStreamFull.h>
miller86's avatar
miller86 committed
40
#include <DebugStream.h>
41
#include <visitstream.h>
hrchilds's avatar
hrchilds committed
42 43 44 45

#if defined(_WIN32)
#include <windows.h>
#else
46
#include <sys/types.h>
hrchilds's avatar
hrchilds committed
47 48
#include <unistd.h>
#endif
49
#include <sys/stat.h>
hrchilds's avatar
hrchilds committed
50 51 52 53 54 55 56
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <visit-config.h>
using std::vector;

// static DebugStreamBuf class data
miller86's avatar
miller86 committed
57 58
vector<DebugStreamFull::DebugStreamBuf*> DebugStreamFull::DebugStreamBuf::allBuffers;
int DebugStreamFull::DebugStreamBuf::curLevel;
hrchilds's avatar
hrchilds committed
59 60

// global DebugStreams
61
// We make these static so they are NOT visible outside this file
miller86's avatar
miller86 committed
62 63 64 65 66 67 68 69 70 71 72 73
static DebugStreamFull debug1_realobj(1);
static DebugStreamFull debug2_realobj(2);
static DebugStreamFull debug3_realobj(3);
static DebugStreamFull debug4_realobj(4);
static DebugStreamFull debug5_realobj(5);

bool DebugStream::Level1() { return debug1_realobj.isenabled(); };
bool DebugStream::Level2() { return debug2_realobj.isenabled(); };
bool DebugStream::Level3() { return debug3_realobj.isenabled(); };
bool DebugStream::Level4() { return debug4_realobj.isenabled(); };
bool DebugStream::Level5() { return debug5_realobj.isenabled(); };

74 75 76 77 78 79 80 81 82 83 84
ostream& DebugStream::Stream1(char const *__file__, int __line__)
{
    if (debug1_realobj.isdecorated() && __file__)
    {
        *((ostream*) &debug1_realobj) << __file__;
        if (debug1_realobj.isdecorated() && __line__ > -1)
            *((ostream*) &debug1_realobj) << ":" << __line__;
        *((ostream*) &debug1_realobj) << " ";
    }
    return *((ostream*) &debug1_realobj);
};
miller86's avatar
miller86 committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
ostream& DebugStream::Stream2() { return *((ostream*) &debug2_realobj); };
ostream& DebugStream::Stream3() { return *((ostream*) &debug3_realobj); };
ostream& DebugStream::Stream4() { return *((ostream*) &debug4_realobj); };
ostream& DebugStream::Stream5() { return *((ostream*) &debug5_realobj); };

int
DebugStream::GetLevel()
{
    int level = 0;
    level += debug1_realobj.isenabled();
    level += debug2_realobj.isenabled();
    level += debug3_realobj.isenabled();
    level += debug4_realobj.isenabled();
    level += debug5_realobj.isenabled();
    return level;
}
hrchilds's avatar
hrchilds committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

// ****************************************************************************
// Function: close_streams
//
// Purpose: 
//   Closes the debug streams.
//
// Note:       Taken from code in the signal handlers.
//
// Programmer: Brad Whitlock
// Creation:   Mon Apr 28 10:19:51 PDT 2003
//
// Modifications:
//   
// ****************************************************************************

static void
close_streams()
{
miller86's avatar
miller86 committed
120 121 122 123 124
    if (debug1_realobj.isenabled()) debug1_realobj.close();
    if (debug2_realobj.isenabled()) debug2_realobj.close();
    if (debug3_realobj.isenabled()) debug3_realobj.close();
    if (debug4_realobj.isenabled()) debug4_realobj.close();
    if (debug5_realobj.isenabled()) debug5_realobj.close();
hrchilds's avatar
hrchilds committed
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
}

// ****************************************************************************
//  Function:  signalhandler_core
//
//  Purpose:
//    signal handler for signals which cause a core file to be created
//    flush and close the error logs
//
//  Arguments:
//    sig         the signal caught
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Eric Brugger, Thu Feb 22 12:00:06 PST 2001
//    I removed a reference to SIGEMT since it isn't portable.
//
//    Jeremy Meredith, Thu May 10 13:03:27 PDT 2001
//    Prevent infinite loops from errors on close.
//
//    Jeremy Meredith, Fri Dec 14 12:28:50 PST 2001
//    Check to make sure a debugstream is open before closing it.
//
//    Brad Whitlock, Thu Mar 14 11:54:34 PDT 2002.
//    Added conditional compilation for certain signals that are not
//    defined on Windows.
//
//    Brad Whitlock, Mon Apr 28 10:20:26 PDT 2003
//    I made it print the name of the signal to the debug log so we have
//    an idea of whether or not VisIt quit due to handling a signal.
//
// ****************************************************************************

static void
signalhandler_core(int sig)
{
    // don't catch the abort!
    signal(SIGABRT,SIG_DFL);

    // also prevent infinite loops
    signal(sig,SIG_DFL);

    switch (sig)
    {
      case SIGILL:
          debug1 << "signalhandler_core: SIG!" << endl;
173
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
174 175 176
          break;
      case SIGABRT:
          debug1 << "signalhandler_core: SIGBRT!" << endl;
177
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
178 179 180
          break;
      case SIGFPE:
          debug1 << "signalhandler_core: SIGFPE!" << endl;
181
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
182 183 184
          break;
      case SIGSEGV:
          debug1 << "signalhandler_core: SIGSEGV!" << endl;
185
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
186 187 188 189
          break;
#if !defined(_WIN32)
      case SIGBUS:
          debug1 << "signalhandler_core: SIGBUS!" << endl;
190
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
191 192 193
          break;
      case SIGQUIT:
          debug1 << "signalhandler_core: SIGQUIT!" << endl;
194
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
195 196 197
          break;
      case SIGTRAP:
          debug1 << "signalhandler_core: SIGTRAP!" << endl;
198
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
199 200 201
          break;
      case SIGSYS:
          debug1 << "signalhandler_core: SIGSYS!" << endl;
202
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
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
          break;
#endif
    }
}

// ****************************************************************************
//  Function:  signalhandler_exit
//
//  Purpose:
//    signal handler for signals which cause the program to exit
//    flush and close the error logs
//
//  Arguments:
//    sig         the signal caught
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Jeremy Meredith, Fri Dec 14 12:28:50 PST 2001
//    Check to make sure a debugstream is open before closing it.
//
//    Brad Whitlock, Thu Mar 14 11:58:34 PDT 2002
//    Added conditional compilation for Windows.
//
//    Brad Whitlock, Mon Apr 28 10:19:09 PDT 2003
//    I added code to print the name of the signal that's causing VisIt
//    to exit.
//
// ****************************************************************************

static void
signalhandler_exit(int sig)
{
    switch (sig)
    {
      case SIGTERM:
          debug1 << "signalhandler_exit: SIGTERM!" << endl;
241
          close_streams(); exit(-1); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
242 243 244
          break;
      case SIGINT:
          debug1 << "signalhandler_exit: SIGINT!" << endl;
245
          close_streams(); exit(-1); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
          break;
    }
}


// ****************************************************************************
// ****************************************************************************
//                         class DebugStreamBuf
// ****************************************************************************
// ****************************************************************************


// ****************************************************************************
//  Constructor:  DebugStreamBuf::DebugStreamBuf
//
//  Purpose:
//    constructor for DebugStreamBuf
//
//  Arguments:
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
// ****************************************************************************
miller86's avatar
miller86 committed
270
DebugStreamFull::DebugStreamBuf::DebugStreamBuf() : streambuf()
hrchilds's avatar
hrchilds committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
{
    allBuffers.push_back(this);
    level = 0;
    out = NULL;
}


// ****************************************************************************
//  Destructor:  DebugStreamBuf::~DebugStreamBuf
//
//  Purpose:
//    destructor for DebugStreamBuf
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
// ****************************************************************************
miller86's avatar
miller86 committed
288
DebugStreamFull::DebugStreamBuf::~DebugStreamBuf()
hrchilds's avatar
hrchilds committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
{
    close();
}


// ****************************************************************************
//  Method:  DebugStreamBuf::SetLevel
//
//  Purpose:
//    set the debug level of this streambuf
//
//  Arguments:
//    level_     the new level
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
// ****************************************************************************
void
miller86's avatar
miller86 committed
308
DebugStreamFull::DebugStreamBuf::SetLevel(int level_)
hrchilds's avatar
hrchilds committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
    level=level_;
}


// ****************************************************************************
//  Method:  DebugStreamBuf::close
//
//  Purpose:
//    safely close the file and delete it if necessary
//
//  Arguments:
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
// ****************************************************************************
void
miller86's avatar
miller86 committed
327
DebugStreamFull::DebugStreamBuf::close()
hrchilds's avatar
hrchilds committed
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
{
    if (out)
    {
        out->close();
        delete out;
        out = NULL;
    }
}


// ****************************************************************************
//  Method:  DebugStreamBuf::open
//
//  Purpose:
//    open a file and set its buffering
//
//  Arguments:
//    filename_    the filename of the file to create
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Jeremy Meredith, Fri Jun  1 17:25:27 PDT 2001
//    Made *all* DebugStreams unit buffered.
//
//    Jeremy Meredith, Fri Dec 14 12:27:18 PST 2001
//    Unit-buffering them was not working on SunOS.  I added a call
//    to force unit-buffering on the streambuf, maybe even unbuffered.
//
//    Jeremy Meredith, Mon Apr 15 12:19:37 PDT 2002
//    Apparently setbuf is not public on some platforms.  I added an #ifdef
//    to disable it based on a configure'd define.
//
362 363
//    Mark C. Miller, Tue Apr 14 16:01:49 PDT 2009
//    Added option to buffer the debug logs.
hrchilds's avatar
hrchilds committed
364 365
// ****************************************************************************
void
miller86's avatar
miller86 committed
366
DebugStreamFull::DebugStreamBuf::open(const char *filename_, bool buffer_debug)
hrchilds's avatar
hrchilds committed
367 368 369 370 371 372 373 374 375 376 377 378 379
{
    close();
    strcpy(filename, filename_);
    out = new ofstream;
    out->open(filename, ios::out);
    if (! *out)
    {
        delete out;
        out = NULL;
    }
    else
    {
        // flush the buffer after every operation
380 381
        if (!buffer_debug)
            out->setf(ios::unitbuf);
hrchilds's avatar
hrchilds committed
382 383 384
#ifndef NO_SETBUF
        // the previous flag does nothing on SunOS;
        // I hate to do this, but I'm doing it to force automatic flushing:
385 386
        if (!buffer_debug)
            out->rdbuf()->setbuf((char*)0,0);
hrchilds's avatar
hrchilds committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
#endif
    }
}


// ****************************************************************************
//  Method:  DebugStreamBuf::put
//
//  Purpose:
//    write a character to the file if the current level is at least at the
//    priority of this streambuf's level
//
//  Arguments:
//    c         the character
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Brad Whitlock, Thu Mar 14 12:08:34 PST 2002
//    Added a cast.
//
// ****************************************************************************
int
miller86's avatar
miller86 committed
411
DebugStreamFull::DebugStreamBuf::put(int c)
hrchilds's avatar
hrchilds committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
{
    if (out &&
        curLevel <= level)
    {
        if (c!=EOF)
        {
            out->put((char)c);
        }
    }
    return c;
}


// ****************************************************************************
//  Method:  DebugStreamBuf::overflow
//
//  Purpose:
//    this is the routine called by the owning ostream for unbuffered output...
//    we never set up buffered output since we can buffer (if we want to)
//    through the ofstream we are really using to write
//
//  Arguments:
//    c         the character to write
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
// ****************************************************************************
int
miller86's avatar
miller86 committed
441
DebugStreamFull::DebugStreamBuf::overflow(int c)
hrchilds's avatar
hrchilds committed
442 443
{
    curLevel = level;
444
    for (size_t i=0; i<allBuffers.size(); i++)
hrchilds's avatar
hrchilds committed
445 446 447 448 449 450 451 452
        allBuffers[i]->put(c);
    return c;
}



// ****************************************************************************
// ****************************************************************************
miller86's avatar
miller86 committed
453
//                           class DebugStreamFull
hrchilds's avatar
hrchilds committed
454 455 456 457 458
// ****************************************************************************
// ****************************************************************************


// ****************************************************************************
miller86's avatar
miller86 committed
459
//  Constructor:  DebugStreamFull::DebugStreamFull
hrchilds's avatar
hrchilds committed
460 461
//
//  Purpose:
miller86's avatar
miller86 committed
462
//    constructor for the DebugStreamFull
hrchilds's avatar
hrchilds committed
463 464 465 466 467 468 469 470 471 472 473
//
//  Arguments:
//    level_    the debug level of this ostream
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Jeremy Meredith, Fri Jun  1 17:18:46 PDT 2001
//    Added initialization of 'enabled'.
//
hrchilds's avatar
hrchilds committed
474 475 476 477
//    Eric Brugger, Tue Aug  3 11:03:38 PDT 2004
//    Change the DebugStreamBuf member to be a pointered value instead of a
//    referenced value so that it works with the MIPSpro compiler.
//
hrchilds's avatar
hrchilds committed
478
// ****************************************************************************
miller86's avatar
miller86 committed
479
DebugStreamFull::DebugStreamFull(int level_) : ostream(new DebugStreamBuf)
hrchilds's avatar
hrchilds committed
480 481
{
    level = level_;
hrchilds's avatar
hrchilds committed
482 483
    buf = (DebugStreamBuf*)(rdbuf());
    buf->SetLevel(level);
hrchilds's avatar
hrchilds committed
484
    enabled = false;
485
    decorate = false;
hrchilds's avatar
hrchilds committed
486 487 488 489
}


// ****************************************************************************
miller86's avatar
miller86 committed
490
//  Destructor:  DebugStreamFull::~DebugStreamFull
hrchilds's avatar
hrchilds committed
491 492
//
//  Purpose:
miller86's avatar
miller86 committed
493
//    destructor for the DebugStreamFull
hrchilds's avatar
hrchilds committed
494 495 496 497
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
hrchilds's avatar
hrchilds committed
498 499 500 501 502
//  Modifications:
//    Eric Brugger, Tue Aug  3 11:03:38 PDT 2004
//    Change the DebugStreamBuf member to be a pointered value instead of a
//    referenced value so that it works with the MIPSpro compiler.
//
503 504 505
//    Mark C. Miller, Tue Jan 11 09:27:18 PST 2011
//    Added missing call to delete buf. Since above change in 2004, we have 
//    been leaking these bufs on exit.
hrchilds's avatar
hrchilds committed
506
// ****************************************************************************
miller86's avatar
miller86 committed
507
DebugStreamFull::~DebugStreamFull()
hrchilds's avatar
hrchilds committed
508
{
hrchilds's avatar
hrchilds committed
509
    buf->close();
510
    delete buf;
hrchilds's avatar
hrchilds committed
511 512 513 514
}


// ****************************************************************************
miller86's avatar
miller86 committed
515
//  Method:  DebugStreamFull::open
hrchilds's avatar
hrchilds committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529
//
//  Purpose:
//    create a file for logging given the program name
//
//  Arguments:
//    progname   the name of the program executing
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Jeremy Meredith, Fri Jun  1 17:25:01 PDT 2001
//    Added setting of 'enabled' to true.
//
hrchilds's avatar
hrchilds committed
530 531 532 533
//    Eric Brugger, Tue Aug  3 11:03:38 PDT 2004
//    Change the DebugStreamBuf member to be a pointered value instead of a
//    referenced value so that it works with the MIPSpro compiler.
//
534 535 536 537
//    Mark C. Miller, Thu Apr  3 13:39:55 PDT 2008
//    Change the extension from '.log' to '.vlog'. Made it loop to find
//    a unique filename.
//
538 539 540 541 542
//    Mark C. Miller, Thu Apr 10 08:07:52 PDT 2008
//    Changed order of debug level and run number in filename.
//    Changed strspn to scan progname, not filename (which would already
//    have a 'level' sprintf'd into it.
//
543 544 545 546 547 548 549 550
//    Mark C. Miller, Thu Mar 12 18:20:34 PDT 2009
//    Changed the case of existing, older vlogs are handled. Before, it would
//    create a unique name by incrimenting a counter. Now, it will maintain
//    the 5 most recent logs named A.progname.level.vlog, B.progname.level.vlog
//    etc. with A always being the most recent and E the least recent. 
//    This has some undesirable behavior if you happening to be editing a vlog
//    and re-run VisIt as it may cause the editor to save a swap file because
//    the file's name is getting changed out from underneath the editor.
551 552 553
//
//    Mark C. Miller, Tue Apr 14 16:01:49 PDT 2009
//    Added option to buffer the debug logs.
554 555 556 557 558 559
//
//    Kathleen Bonnell, Thu Jan 21 07:44:15 MST 2009 
//    Changed naming on windows to NOT prepend a letter, due to fact that
//    progname generally contains fully-qualified-filename, and the fact that
//    we always use pids on windows, so renaming not necessary.
//
hrchilds's avatar
hrchilds committed
560
// ****************************************************************************
561 562


hrchilds's avatar
hrchilds committed
563
void
miller86's avatar
miller86 committed
564
DebugStreamFull::open(const char *progname, bool clobber, bool buffer_debug)
hrchilds's avatar
hrchilds committed
565 566
{
    char filename[256];
567 568 569 570 571 572 573

#ifdef WIN32
    // On windows, we always use pids, so won't need to rename, and thus
    // don't need to prepend a letter.
    sprintf(filename, "%s.%d.vlog", progname, level);

#else
574
    sprintf(filename, "A.%s.%d.vlog", progname, level);
575

576 577
    // only rename old vlogs if we don't have pids
    bool renameOld = !clobber && (strspn(progname, ".0123456789") == 0);
578

579 580
    // Move all older filenames by one letter
    if (renameOld)
581
    {
582 583 584 585 586 587 588 589 590 591 592 593
        char filenametmp1[256];
        char filenametmp2[256];
        sprintf(filenametmp1, "E.%s.%d.vlog", progname, level);
        unlink(filenametmp1);               // E->deleted 
        sprintf(filenametmp2, "D.%s.%d.vlog", progname, level);
        rename(filenametmp2, filenametmp1); // D->E
        sprintf(filenametmp1, "C.%s.%d.vlog", progname, level);
        rename(filenametmp1, filenametmp2); // C->D
        sprintf(filenametmp2, "B.%s.%d.vlog", progname, level);
        rename(filenametmp2, filenametmp1); // B->C
        sprintf(filenametmp1, "A.%s.%d.vlog", progname, level);
        rename(filenametmp1, filenametmp2); // A->B
594
    }
595
#endif
596 597

    // ok, open the stream
598
    buf->open(filename, buffer_debug);
hrchilds's avatar
hrchilds committed
599 600 601 602 603
    enabled = true;
}


// ****************************************************************************
miller86's avatar
miller86 committed
604
//  Method:  DebugStreamFull::close
hrchilds's avatar
hrchilds committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618
//
//  Purpose:
//    close the file, but don't delete it
//
//  Arguments:
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Jeremy Meredith, Fri Dec 14 12:26:46 PST 2001
//    Added setting enabled to false so we won't try to close it
//    more than once.
//
hrchilds's avatar
hrchilds committed
619 620 621 622
//    Eric Brugger, Tue Aug  3 11:03:38 PDT 2004
//    Change the DebugStreamBuf member to be a pointered value instead of a
//    referenced value so that it works with the MIPSpro compiler.
//
hrchilds's avatar
hrchilds committed
623 624
// ****************************************************************************
void
miller86's avatar
miller86 committed
625
DebugStreamFull::close()
hrchilds's avatar
hrchilds committed
626
{
hrchilds's avatar
hrchilds committed
627
    buf->close();
hrchilds's avatar
hrchilds committed
628 629 630 631
    enabled = false;
}

// ****************************************************************************
miller86's avatar
miller86 committed
632
//  Method:  DebugStreamFull::Initialize
hrchilds's avatar
hrchilds committed
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
//
//  Purpose:
//    This should be called once to initalize all the debug streams.
//    Also registers signal handlers to flush the output of all the log files
//    on abnormal program termination.
//
//  Arguments:
//    progname     the name of the running program
//    debuglevel   1-5: the lowest priority of messages you want to log
//                 0: same as 1, but hide the log and delete it on normal exit
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
//  Modifications:
//    Eric Brugger, Thu Feb 22 12:00:06 PST 2001
//    I removed a reference to SIGEMT since it isn't portable.
//
//    Hank Childs, Fri Mar  2 14:53:17 PST 2001
//    Fixed bug with prefixing log name with a '.'.
//
//    Brad Whitlock, Thu Mar 14 11:59:34 PDT 2002
//    Added conditional compilation for Windows.
//
//    Jeremy Meredith, Wed Aug  7 13:17:22 PDT 2002
//    Made it not puke on interesting debug levels.
//
//    Hank Childs, Wed Nov 13 08:28:41 PST 2002
//    Ignore FPE, since they come up on gps (and MeshTV ignores them too).
//
//    Brad Whitlock, Mon Apr 28 10:18:01 PDT 2003
//    Ignore SIGPIPE because it can defeat our mechanisms for detecting when
//    we lose a connection to a component.
//
hrchilds's avatar
hrchilds committed
667 668 669
//    Jeremy Meredith, Tue May 17 11:20:51 PDT 2005
//    Allow disabling of signal handlers.
//
670 671
//    Mark C. Miller, Tue Apr 14 16:01:49 PDT 2009
//    Added option to buffer the debug logs.
hrchilds's avatar
hrchilds committed
672 673 674
// ****************************************************************************

void
miller86's avatar
miller86 committed
675
DebugStreamFull::Initialize(const char *progname, int debuglevel, bool sigs,
676
    bool clobber, bool buffer_debug, bool _decorate)
hrchilds's avatar
hrchilds committed
677 678 679
{
    switch (debuglevel)
    {
680
      case 5:  debug5_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
681
        /* FALLTHRU */
682
      case 4:  debug4_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
683
        /* FALLTHRU */
684
      case 3:  debug3_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
685
        /* FALLTHRU */
686
      case 2:  debug2_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
687
        /* FALLTHRU */
688
      case 1:  debug1_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
689 690 691 692
        break;
      default:
        break;
    }
693
    debug1_realobj.decorate = _decorate;
hrchilds's avatar
hrchilds committed
694

hrchilds's avatar
hrchilds committed
695 696
    if (sigs)
    {
hrchilds's avatar
hrchilds committed
697
#if !defined(_WIN32)
hrchilds's avatar
hrchilds committed
698 699 700 701 702
        signal(SIGQUIT,  signalhandler_core);
        signal(SIGTRAP,  signalhandler_core);
        signal(SIGSYS,   signalhandler_core);
        signal(SIGBUS,   signalhandler_core);
        signal(SIGPIPE,  SIG_IGN);
hrchilds's avatar
hrchilds committed
703 704
#endif

hrchilds's avatar
hrchilds committed
705 706 707 708 709 710 711
        signal(SIGILL,   signalhandler_core);
        signal(SIGABRT,  signalhandler_core);
        signal(SIGFPE,   SIG_IGN);
        signal(SIGSEGV,  signalhandler_core);
        signal(SIGTERM,  signalhandler_exit);
        signal(SIGINT,   signalhandler_exit);
    }
hrchilds's avatar
hrchilds committed
712
}