DebugStreamFull.C 25.3 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>
42
#include <VisItInit.h>
hrchilds's avatar
hrchilds committed
43 44 45 46

#if defined(_WIN32)
#include <windows.h>
#else
47
#include <sys/types.h>
hrchilds's avatar
hrchilds committed
48 49
#include <unistd.h>
#endif
50
#include <sys/stat.h>
hrchilds's avatar
hrchilds committed
51 52 53 54 55 56 57
#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
58 59
vector<DebugStreamFull::DebugStreamBuf*> DebugStreamFull::DebugStreamBuf::allBuffers;
int DebugStreamFull::DebugStreamBuf::curLevel;
hrchilds's avatar
hrchilds committed
60

61 62
int DebugStreamFull::numThreadLogs = 1;

hrchilds's avatar
hrchilds committed
63
// global DebugStreams
64
// We make these static so they are NOT visible outside this file
miller86's avatar
miller86 committed
65 66 67 68 69 70
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);

71 72 73 74 75 76 77 78
bool IsThread0(void)
{
    if (VisItInit::GetNumberOfThreads() <= 1)
        return true;
    if (VisItInit::GetMyThreadID() == 0)
        return true;
    return false;
}
miller86's avatar
miller86 committed
79 80 81 82 83 84
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(); };

85 86 87 88 89 90 91 92 93 94 95
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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

// ****************************************************************************
// 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
131 132 133 134 135
    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
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
}

// ****************************************************************************
//  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;
184
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
185 186 187
          break;
      case SIGABRT:
          debug1 << "signalhandler_core: SIGBRT!" << endl;
188
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
189 190 191
          break;
      case SIGFPE:
          debug1 << "signalhandler_core: SIGFPE!" << endl;
192
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
193 194 195
          break;
      case SIGSEGV:
          debug1 << "signalhandler_core: SIGSEGV!" << endl;
196
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
197 198 199 200
          break;
#if !defined(_WIN32)
      case SIGBUS:
          debug1 << "signalhandler_core: SIGBUS!" << endl;
201
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
202 203 204
          break;
      case SIGQUIT:
          debug1 << "signalhandler_core: SIGQUIT!" << endl;
205
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
206 207 208
          break;
      case SIGTRAP:
          debug1 << "signalhandler_core: SIGTRAP!" << endl;
209
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
210 211 212
          break;
      case SIGSYS:
          debug1 << "signalhandler_core: SIGSYS!" << endl;
213
          close_streams(); abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
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
          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;
252
          close_streams(); exit(-1); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
253 254 255
          break;
      case SIGINT:
          debug1 << "signalhandler_exit: SIGINT!" << endl;
256
          close_streams(); exit(-1); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
          break;
    }
}


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


// ****************************************************************************
//  Constructor:  DebugStreamBuf::DebugStreamBuf
//
//  Purpose:
//    constructor for DebugStreamBuf
//
//  Arguments:
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
// ****************************************************************************
miller86's avatar
miller86 committed
281
DebugStreamFull::DebugStreamBuf::DebugStreamBuf() : streambuf()
hrchilds's avatar
hrchilds committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
{
    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
299
DebugStreamFull::DebugStreamBuf::~DebugStreamBuf()
hrchilds's avatar
hrchilds committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
    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
319
DebugStreamFull::DebugStreamBuf::SetLevel(int level_)
hrchilds's avatar
hrchilds committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
{
    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
338
DebugStreamFull::DebugStreamBuf::close()
hrchilds's avatar
hrchilds committed
339
{
340 341 342 343 344 345 346
    if(out == NULL) return;

    for(int i=0; i<numThreadLogs; ++i) 
        if (out[i])
            out[i].close();
 
    if(out) 
hrchilds's avatar
hrchilds committed
347
    {
348
        delete [] out;
hrchilds's avatar
hrchilds committed
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
        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.
//
378 379
//    Mark C. Miller, Tue Apr 14 16:01:49 PDT 2009
//    Added option to buffer the debug logs.
hrchilds's avatar
hrchilds committed
380 381
// ****************************************************************************
void
miller86's avatar
miller86 committed
382
DebugStreamFull::DebugStreamBuf::open(const char *filename_, bool buffer_debug)
hrchilds's avatar
hrchilds committed
383 384 385
{
    close();
    strcpy(filename, filename_);
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    out = new ofstream[numThreadLogs];
 
    for(int i=0; i<numThreadLogs; ++i) {
        std::stringstream fname;
        fname << filename << i << ".vlog";
        out[i].open(fname.str().c_str(), ios::out);
        if (! out[i])
        {
            //out[i] is a value, not a pointer....
            //delete [] out;
            //out = NULL;
        }
        else
        {
            // flush the buffer after every operation
            if (!buffer_debug)
                out[i].setf(ios::unitbuf);

hrchilds's avatar
hrchilds committed
404
#ifndef NO_SETBUF
405 406 407 408
            // the previous flag does nothing on SunOS;
            // I hate to do this, but I'm doing it to force automatic flushing:
            if (!buffer_debug)
                out[i].rdbuf()->setbuf((char*)0,0);
hrchilds's avatar
hrchilds committed
409
#endif
410
        }
hrchilds's avatar
hrchilds committed
411
    }
412
    
hrchilds's avatar
hrchilds committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}


// ****************************************************************************
//  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
435
DebugStreamFull::DebugStreamBuf::put(int c)
hrchilds's avatar
hrchilds committed
436
{
437 438
    int t_id = VisItInit::GetMyThreadID();
    if ( t_id < numThreadLogs && out != NULL && out[t_id] &&
hrchilds's avatar
hrchilds committed
439 440 441 442
        curLevel <= level)
    {
        if (c!=EOF)
        {
443
            out[t_id].put((char)c);
hrchilds's avatar
hrchilds committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
        }
    }
    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
466
DebugStreamFull::DebugStreamBuf::overflow(int c)
hrchilds's avatar
hrchilds committed
467 468
{
    curLevel = level;
469
    for (size_t i=0; i<allBuffers.size(); i++)
hrchilds's avatar
hrchilds committed
470 471 472 473 474 475 476 477
        allBuffers[i]->put(c);
    return c;
}



// ****************************************************************************
// ****************************************************************************
miller86's avatar
miller86 committed
478
//                           class DebugStreamFull
hrchilds's avatar
hrchilds committed
479 480 481 482 483
// ****************************************************************************
// ****************************************************************************


// ****************************************************************************
miller86's avatar
miller86 committed
484
//  Constructor:  DebugStreamFull::DebugStreamFull
hrchilds's avatar
hrchilds committed
485 486
//
//  Purpose:
miller86's avatar
miller86 committed
487
//    constructor for the DebugStreamFull
hrchilds's avatar
hrchilds committed
488 489 490 491 492 493 494 495 496 497 498
//
//  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
499 500 501 502
//    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
503
// ****************************************************************************
miller86's avatar
miller86 committed
504
DebugStreamFull::DebugStreamFull(int level_) : ostream(new DebugStreamBuf)
hrchilds's avatar
hrchilds committed
505 506
{
    level = level_;
hrchilds's avatar
hrchilds committed
507 508
    buf = (DebugStreamBuf*)(rdbuf());
    buf->SetLevel(level);
hrchilds's avatar
hrchilds committed
509
    enabled = false;
510
    decorate = false;
hrchilds's avatar
hrchilds committed
511 512 513 514
}


// ****************************************************************************
miller86's avatar
miller86 committed
515
//  Destructor:  DebugStreamFull::~DebugStreamFull
hrchilds's avatar
hrchilds committed
516 517
//
//  Purpose:
miller86's avatar
miller86 committed
518
//    destructor for the DebugStreamFull
hrchilds's avatar
hrchilds committed
519 520 521 522
//
//  Programmer:  Jeremy Meredith
//  Creation:    November 17, 2000
//
hrchilds's avatar
hrchilds committed
523 524 525 526 527
//  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.
//
528 529 530
//    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
531
// ****************************************************************************
miller86's avatar
miller86 committed
532
DebugStreamFull::~DebugStreamFull()
hrchilds's avatar
hrchilds committed
533
{
hrchilds's avatar
hrchilds committed
534
    buf->close();
535
    delete buf;
hrchilds's avatar
hrchilds committed
536 537 538 539
}


// ****************************************************************************
miller86's avatar
miller86 committed
540
//  Method:  DebugStreamFull::open
hrchilds's avatar
hrchilds committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554
//
//  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
555 556 557 558
//    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.
//
559 560 561 562
//    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.
//
563 564 565 566 567
//    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.
//
568 569 570 571 572 573 574 575
//    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.
576 577 578
//
//    Mark C. Miller, Tue Apr 14 16:01:49 PDT 2009
//    Added option to buffer the debug logs.
579 580 581 582 583 584
//
//    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
585
// ****************************************************************************
586 587


hrchilds's avatar
hrchilds committed
588
void
miller86's avatar
miller86 committed
589
DebugStreamFull::open(const char *progname, bool clobber, bool buffer_debug)
hrchilds's avatar
hrchilds committed
590 591
{
    char filename[256];
592 593 594 595

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

#else
599
    sprintf(filename, "A.%s.%d.thr", progname, level);
600

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

604 605
    // Move all older filenames by one letter
    if (renameOld)
606
    {
607 608
        char filenametmp1[256];
        char filenametmp2[256];
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
        int fileThr = 0;
        sprintf(filenametmp1, "E.%s.%d.thr%d.vlog", progname, level, fileThr);
        while (access( filenametmp1, F_OK) != -1 )
        {
             unlink(filenametmp1);               // E->deleted 
             sprintf(filenametmp1, "E.%s.%d.thr%d.vlog", progname, level, ++fileThr);
        }
        fileThr = 0;
        sprintf(filenametmp1, "E.%s.%d.thr%d.vlog", progname, level, fileThr);
        sprintf(filenametmp2, "D.%s.%d.thr%d.vlog", progname, level, fileThr);
        while (access( filenametmp2, F_OK) != -1 )
        {
            rename(filenametmp2, filenametmp1); // D->E
 
            ++fileThr;
            sprintf(filenametmp1, "E.%s.%d.thr%d.vlog", progname, level, fileThr);
            sprintf(filenametmp2, "D.%s.%d.thr%d.vlog", progname, level, fileThr);
        }
        fileThr = 0;
        sprintf(filenametmp1, "D.%s.%d.thr%d.vlog", progname, level, fileThr);
        sprintf(filenametmp2, "C.%s.%d.thr%d.vlog", progname, level, fileThr);
        while (access( filenametmp2, F_OK) != -1 )
        {
            rename(filenametmp2, filenametmp1); // C->D
 
            ++fileThr;
            sprintf(filenametmp1, "D.%s.%d.thr%d.vlog", progname, level, fileThr);
            sprintf(filenametmp2, "C.%s.%d.thr%d.vlog", progname, level, fileThr);
        }
        fileThr = 0;
        sprintf(filenametmp1, "C.%s.%d.thr%d.vlog", progname, level, fileThr);
        sprintf(filenametmp2, "B.%s.%d.thr%d.vlog", progname, level, fileThr);
        while (access( filenametmp2, F_OK) != -1 )
        {
            rename(filenametmp2, filenametmp1); // B->C
 
            ++fileThr;
            sprintf(filenametmp1, "C.%s.%d.thr%d.vlog", progname, level, fileThr);
            sprintf(filenametmp2, "B.%s.%d.thr%d.vlog", progname, level, fileThr);
        }
        fileThr = 0;
        sprintf(filenametmp1, "B.%s.%d.thr%d.vlog", progname, level, fileThr);
        sprintf(filenametmp2, "A.%s.%d.thr%d.vlog", progname, level, fileThr);
        while (access( filenametmp2, F_OK) != -1 )
        {
            rename(filenametmp2, filenametmp1); // A->B
 
            ++fileThr;
            sprintf(filenametmp1, "B.%s.%d.thr%d.vlog", progname, level, fileThr);
            sprintf(filenametmp2, "A.%s.%d.thr%d.vlog", progname, level, fileThr);
        }
660
    }
661
#endif
662 663

    // ok, open the stream
664
    buf->open(filename, buffer_debug);
hrchilds's avatar
hrchilds committed
665 666 667 668 669
    enabled = true;
}


// ****************************************************************************
miller86's avatar
miller86 committed
670
//  Method:  DebugStreamFull::close
hrchilds's avatar
hrchilds committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684
//
//  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
685 686 687 688
//    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
689 690
// ****************************************************************************
void
miller86's avatar
miller86 committed
691
DebugStreamFull::close()
hrchilds's avatar
hrchilds committed
692
{
hrchilds's avatar
hrchilds committed
693
    buf->close();
hrchilds's avatar
hrchilds committed
694 695 696 697
    enabled = false;
}

// ****************************************************************************
miller86's avatar
miller86 committed
698
//  Method:  DebugStreamFull::Initialize
hrchilds's avatar
hrchilds committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
//
//  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
733 734 735
//    Jeremy Meredith, Tue May 17 11:20:51 PDT 2005
//    Allow disabling of signal handlers.
//
736 737
//    Mark C. Miller, Tue Apr 14 16:01:49 PDT 2009
//    Added option to buffer the debug logs.
738 739 740
//
//    Elliott Ewing, Fri Oct 3 16:10:05 PDT 2015
//    Added argument to specify number of threaded logs from command line args.
hrchilds's avatar
hrchilds committed
741 742 743
// ****************************************************************************

void
744
DebugStreamFull::Initialize(const char *progname, int debuglevel, int numThreadLogs_, bool sigs,
745
    bool clobber, bool buffer_debug, bool _decorate)
hrchilds's avatar
hrchilds committed
746
{
747 748 749 750 751
    if(numThreadLogs_ >= VisItInit::GetNumberOfThreads())
        numThreadLogs = VisItInit::GetNumberOfThreads();
    else 
        numThreadLogs = numThreadLogs_;
 
hrchilds's avatar
hrchilds committed
752 753
    switch (debuglevel)
    {
754
      case 5:  debug5_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
755
        /* FALLTHRU */
756
      case 4:  debug4_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
757
        /* FALLTHRU */
758
      case 3:  debug3_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
759
        /* FALLTHRU */
760
      case 2:  debug2_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
761
        /* FALLTHRU */
762
      case 1:  debug1_realobj.open(progname, clobber, buffer_debug);
hrchilds's avatar
hrchilds committed
763 764 765 766
        break;
      default:
        break;
    }
767
    debug1_realobj.decorate = _decorate;
hrchilds's avatar
hrchilds committed
768

hrchilds's avatar
hrchilds committed
769 770
    if (sigs)
    {
hrchilds's avatar
hrchilds committed
771
#if !defined(_WIN32)
hrchilds's avatar
hrchilds committed
772 773 774 775 776
        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
777 778
#endif

hrchilds's avatar
hrchilds committed
779 780 781 782 783 784 785
        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
786
}