VisItInit.C 21.2 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.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39 40 41 42
// ************************************************************************* //
//                                    Init.C                                 //
// ************************************************************************* //

43
#include <VisItInit.h>
hrchilds's avatar
hrchilds committed
44 45 46 47 48 49 50 51 52 53 54

#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
#include <process.h>
#include <winsock2.h>
#else
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#endif
55
#include <new>
56
#include <cstring>
loring's avatar
loring committed
57 58
#include <sstream>
using std::ostringstream;
hrchilds's avatar
hrchilds committed
59

60
#include <ConfigureInfo.h>
61
#include <DebugStreamFull.h>
miller86's avatar
miller86 committed
62
#include <DebugStream.h>
63
#include <InstallationFunctions.h>
hrchilds's avatar
hrchilds committed
64
#include <TimingsManager.h>
65
#include <visit-config.h>
hrchilds's avatar
hrchilds committed
66 67

static void RemovePrependedDirs(const char *, char *); 
hrchilds's avatar
hrchilds committed
68 69
static char executableName[256];
static char componentName[256];
70 71 72 73 74 75 76
static const char *AVTPREP = "avtprep";
static const char *CLI = "cli";
static const char *ENGINE = "engine";
static const char *GUI = "gui";
static const char *LAUNCHER = "launcher";
static const char *MDSERVER = "mdserver";
static const char *VIEWER = "viewer";
hrchilds's avatar
hrchilds committed
77
static const char *const allComponentNames[] = {
78 79 80 81 82 83 84
    AVTPREP,
    CLI,
    ENGINE,
    GUI,
    LAUNCHER,
    MDSERVER,
    VIEWER
hrchilds's avatar
hrchilds committed
85
};
hrchilds's avatar
hrchilds committed
86 87
static ErrorFunction errorFunction = NULL;
static void *errorFunctionArgs = NULL;
88 89
static bool initializeCalled = false;
static bool finalizeCalled = false;
hrchilds's avatar
hrchilds committed
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

// ****************************************************************************
//  Function: striparg
//
//  Purpose:
//      Given an argc and an argv, strip off the arguments from i to i+n.
//
//  Arguments:
//      i           The first argument to strip      (i)
//      n           The number of arguments to strip (i)
//      argc        The number of total arguments    (io)
//      argv        The array of arguments           (io)
//
//  Programmer: Jeremy Meredith
//  Creation:   November 16, 2000
//
//  Modifications:
//    Jeremy Meredith, Fri Feb  1 16:05:05 PST 2002
//    Fixed a bug where it was copying beyond the end of the array.
//
// ****************************************************************************
static void
striparg(int i, int n, int &argc, char *argv[])
{
    for ( ; i+n<argc; i++)
        argv[i] = argv[i+n];

    argc -= n;
}

// ****************************************************************************
//  Function: NewHandler
//
//  Purpose:
//      Called when the component runs out of memory.
//
//  Programmer: Hank Childs
//  Creation:   August 21, 2001
//
//  Modifications:
//
//    Hank Childs, Tue Nov 20 13:07:51 PST 2001
//    Added an abort.
//
// ****************************************************************************
static void
NewHandler(void)
{
    debug1 << "This component has run out of memory." << endl;
139
    abort(); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
140 141 142
}

// ****************************************************************************
143
//  Function: VisItInit::Initialize
hrchilds's avatar
hrchilds committed
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
//
//  Purpose:
//      Parse any common commandline arguments (e.g. "-debug N") and do
//      common initialization.
//
//  Arguments:
//      argc        The number of total arguments                       (io)
//      argv        The array of arguments                              (io)
//      r           The rank of this process                            (i)
//      n           The total number of processes                       (i)
//      strip       Whether or not to strip the argument from the list. (i)
//
//  Programmer: Jeremy Meredith
//  Creation:   November 16, 2000
//
//  Modifications:
//    Brad Whitlock, Mon Nov 27 16:25:07 PST 2000
//    I added a new flag that indicates whether or not to strip the argument
//    from the list after processing it.
//
//    Hank Childs, Sat Mar 10 15:35:36 PST 2001
//    Start timings manager too.
//
//    Hank Childs, Mon Aug 13 13:43:21 PDT 2001
//    Remove the directory before the program name to allow debug stream to
//    go to current directory.
//
//    Hank Childs, Tue Aug 21 13:05:28 PDT 2001
//    Registered a new handler.
//
//    Jeremy Meredith, Fri Feb  1 16:05:36 PST 2002
//    Added an 'else' to the if test when checking arguments, and added a
//    '--i' when stripping the arg.
//
//    Brad Whitlock, Thu Mar 14 12:43:34 PDT 2002
//    Made it work on Windows. Added code to initialize sockets.
//
//    Jeremy Meredith, Wed Aug  7 13:17:01 PDT 2002
//    Made it clamp the debug level to 0 through 5.
//
hrchilds's avatar
hrchilds committed
184 185 186 187 188
//    Brad Whitlock, Wed May 21 17:03:56 PST 2003
//    I made it write the command line arguments to the debug logs.
//
//    Brad Whitlock, Wed Jun 18 11:15:22 PDT 2003
//    I made it understand the -timing argument.
hrchilds's avatar
hrchilds committed
189
//
hrchilds's avatar
hrchilds committed
190 191 192
//    Hank Childs, Tue Jun  1 11:47:36 PDT 2004
//    Removed atexit call, since that is buggy with gcc.
//
hrchilds's avatar
hrchilds committed
193 194 195
//    Jeremy Meredith, Tue May 17 11:20:51 PDT 2005
//    Allow disabling of signal handlers.
//
hrchilds's avatar
hrchilds committed
196 197 198 199
//    Mark C. Miller, Wed Aug  2 19:58:44 PDT 2006
//    Added explicit call to SetFilename for TimingsManager. This is to
//    handle cases where TimingsManager may have already been instanced
//    before Initialize is called as in the engine.
200 201 202 203
//
//    Kathleen Bonnell, Wed Aug 22 18:00:57 PDT 2007 
//    On Windows, write timings and log files to User's directory. 
//
cyrush's avatar
cyrush committed
204 205 206 207
//    Cyrus Harrison, Tue Sep  4 10:54:59 PDT 2007
//    If -vtk-debug is specified make sure the debug level is at least 1
//    for the engine
//
hrchilds's avatar
hrchilds committed
208 209 210
//    Hank Childs, Tue Dec 18 15:53:10 PST 2007
//    Add support for -svn_revision.
//
211 212 213
//    Cyrus Harrison, Wed Jan 23 09:23:19 PST 2008
//    Changed set_new_handler to std::set_new_handler b/c of change from 
//    deprecated <new.h> to <new>
214 215 216 217 218 219
//
//    Brad Whitlock, Wed Jun 25 10:42:58 PDT 2008
//    Added check to return early in case this function gets called multiple
//    times as when you include multiple VisIt components in a single
//    executable.
//
220 221 222 223
//    Dave Pugmire, Thu Jan 15 16:35:08 EST 2009
//    Add support for -debug_engine_rank, limiting debug output to the specified
//    rank.
//
224 225 226
//    Brad Whitlock, Fri Apr 10 16:00:55 PDT 2009
//    I added support for reading -dv.
//
227 228 229
//    Mark C. Miller, Tue Apr 21 14:24:18 PDT 2009
//    Added logic to manage buffering of debug logs; an extra 'b' after level.
//
whitlocb's avatar
whitlocb committed
230 231 232
//    Brad Whitlock, Thu Jun 18 15:21:06 PDT 2009
//    I added -debug-processor-stride
//
233 234 235 236
//    Kathleen Bonnell, Thu Sep  7 12:02:15 PDT 2010
//    Use InstallationFunction GetUserVisItDirectory instead of retreiving
//    VISITUSERHOME directly from environment (in case it is not set).
//
237 238 239
//    Tom Fogal, Wed Sep 28 13:40:21 MDT 2011
//    Fix a UMR that valgrind complained about.
//
240 241 242 243
//    Mark C. Miller, Thu Sep 10 11:07:15 PDT 2015
//    Added logic to manage decoration of level 1 debug logs with __FILE__
//    and __LINE__; an extra 'd' in debug level arg will turn on these log
//    decorations.
hrchilds's avatar
hrchilds committed
244 245 246
// ****************************************************************************

void
247
VisItInit::Initialize(int &argc, char *argv[], int r, int n, bool strip, bool sigs)
hrchilds's avatar
hrchilds committed
248
{
249 250 251 252 253
    // Return if Initialize has already been called.
    if(initializeCalled)
         return;
    initializeCalled = true;

whitlocb's avatar
whitlocb committed
254
    int i, debuglevel = 0, engineDebugRank=-1, debugStride = 1;
hrchilds's avatar
hrchilds committed
255 256 257
#if defined(_WIN32)
    bool usePid = true;
#else
hrchilds's avatar
hrchilds committed
258
    bool usePid = false;
hrchilds's avatar
hrchilds committed
259
#endif
260
    bool bufferDebug = false;
261
    bool decorateDebug1 = false;
262
    bool clobberVlogs = false;
cyrush's avatar
cyrush committed
263
    bool vtk_debug = false;
hrchilds's avatar
hrchilds committed
264 265
    bool enableTimings = false;
    for (i=1; i<argc; i++)
hrchilds's avatar
hrchilds committed
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
        if (strcmp("-debug_engine_rank", argv[i]) == 0)
        {
            engineDebugRank = -1;
            if (i+1 < argc && isdigit(*(argv[i+1])))
                engineDebugRank = atoi(argv[i+1]);
            else
                cerr<<"Warning: debug engine rank not specified. Igorning.\n";
            if (engineDebugRank >= n)
            {
                cerr<<"Warning: clamping debug engine rank to "<<n-1<<endl;
                engineDebugRank = n-1;
            }
            else if (engineDebugRank < 0)
            {
                cerr<<"Warning: clamping debug engine rank to 0\n";
                engineDebugRank = 0;
            }
            if(strip)
            {
                striparg(i,2, argc,argv);
                --i;
            }
            else
                ++i;
        }

293
        if (strncmp("-debug",argv[i],6) == 0)
hrchilds's avatar
hrchilds committed
294
        {
ahern's avatar
ahern committed
295 296 297
            if ((strlen(argv[i]) > 7 && IsComponent(&argv[i][7])) ||
                 strlen(argv[i]) == 6)
            {
298 299 300 301 302
                debuglevel = 1;
                if (i+1 < argc && isdigit(*(argv[i+1])))
                   debuglevel = atoi(argv[i+1]);
                else
                   cerr << "Warning: debug level not specified, assuming 1\n";
303

304 305 306 307 308 309 310 311 312 313
                if (debuglevel > 5)
                {
                    cerr << "Warning: clamping debug level to 5\n";
                    debuglevel = 5;
                }
                if (debuglevel < 0)
                {
                    cerr << "Warning: clamping debug level to 0\n";
                    debuglevel = 0;
                }
314
                if (i+1 < argc && strchr(argv[i+1],'b'))
315
                    bufferDebug = true;
316 317
                if (i+1 < argc && strchr(argv[i+1],'d'))
                    decorateDebug1 = true;
318

319 320 321 322 323 324 325 326 327
                if(strip)
                {
                    striparg(i,2, argc,argv);
                    --i;
                }
                else
                {
                    ++i;
                }
ahern's avatar
ahern committed
328
            }
329
        }
whitlocb's avatar
whitlocb committed
330 331 332 333 334 335 336 337 338 339
        else if (strcmp("-debug-processor-stride", argv[i]) == 0)
        {
            if(i+1 < argc)
            {
                debugStride = atoi(argv[i+1]);
                ++i;
            }
            else
                cerr << "Warning: debug processor stride not specified." << endl;
        }
340 341 342
        else if (strcmp("-clobber_vlogs", argv[i]) == 0)
        {
            clobberVlogs = true;
hrchilds's avatar
hrchilds committed
343 344 345 346 347
        }
        else if (strcmp("-pid", argv[i]) == 0)
        {
            usePid = true;
        }
cyrush's avatar
cyrush committed
348 349 350 351
        else if (strcmp("-vtk-debug", argv[i]) == 0)
        {
            vtk_debug = true;
        }
hrchilds's avatar
hrchilds committed
352 353 354 355 356
        else if (strcmp("-timing",  argv[i]) == 0 ||
                 strcmp("-timings", argv[i]) == 0)
        {
            enableTimings = true;
        }
357 358 359 360
        else if (strcmp("-dv",  argv[i]) == 0)
        {
            SetIsDevelopmentVersion(true);
        }
hrchilds's avatar
hrchilds committed
361 362
        else if (strcmp("-svn_revision",  argv[i]) == 0)
        {
363
            if(visitcommon::SVNVersion() == "")
hrchilds's avatar
hrchilds committed
364 365
                cerr << "SVN revision is unknown!" << endl;
            else
366
                cerr << "Built from revision " << visitcommon::SVNVersion() << endl;
367
            exit(0); // HOOKS_IGNORE
hrchilds's avatar
hrchilds committed
368
        }
hrchilds's avatar
hrchilds committed
369 370 371 372
    }

    char progname_wo_dir[256];
    RemovePrependedDirs(argv[0], progname_wo_dir);
hrchilds's avatar
hrchilds committed
373 374
    strcpy(executableName, progname_wo_dir);
    strcpy(componentName, progname_wo_dir);
375 376 377
#ifdef WIN32
    // On windows, we want timings and log files to go in user's directory,
    // not install directory, because users may not have write permissions.
378 379
    std::string homedir = GetUserVisItDirectory();
    if(!homedir.empty())
380
    {
381
        if(homedir[homedir.size() - 1] != visitcommon::SlashChar())
382
            homedir += VISIT_SLASH_STRING;
383 384 385 386
        homedir += executableName;
        strcpy(progname_wo_dir, homedir.c_str());
    }
#endif
387
    char progname[256] = {0};
hrchilds's avatar
hrchilds committed
388 389 390 391 392 393 394 395
    if (n > 1)
    {
        sprintf(progname, "%s.%03d", progname_wo_dir, r);
    }
    else
    {
        strcpy(progname, progname_wo_dir);
    }
396
    
hrchilds's avatar
hrchilds committed
397 398 399 400 401 402 403 404 405 406 407 408
    if (usePid)
    {
#if defined(_WIN32)
        int pid = _getpid();
#else
        int pid = (int) getpid();
#endif
        char progname2[256];
        sprintf(progname2, "%s.%d", progname, pid);
        strcpy(progname, progname2);
    }

cyrush's avatar
cyrush committed
409 410 411 412 413
    // if this is the engine and -vtk-debug is enabled, make sure the
    // debuglevel is at least 1
    if(vtk_debug && strstr(progname,"engine") != NULL  && debuglevel < 1)
        debuglevel = 1;

414 415 416 417 418 419 420
    // If debug rank specified, and we are not rank 'r', then turn off debuging.
    if ((strncmp(progname, ENGINE, strlen(ENGINE)) == 0) &&
        engineDebugRank != -1 && engineDebugRank != r)
    {
        debuglevel = 0;
    }
    
whitlocb's avatar
whitlocb committed
421 422 423 424 425 426 427
    // Turn off the processors that don't meet the debug stride.
    if(n > 1 && debuglevel >= 1)
    {
        if((r % debugStride) > 0)
            debuglevel = 0;
    }

hrchilds's avatar
hrchilds committed
428 429
    // Initialize the debug streams and also add the command line arguments
    // to the debug logs.
430 431
    DebugStreamFull::Initialize(progname, debuglevel, sigs, clobberVlogs,
        bufferDebug, decorateDebug1);
loring's avatar
loring committed
432
    ostringstream oss;
hrchilds's avatar
hrchilds committed
433
    for(i = 0; i < argc; ++i)
loring's avatar
loring committed
434 435 436
        oss << argv[i] << " ";
    oss << endl;
    debug1 << oss.str();
hrchilds's avatar
hrchilds committed
437

hrchilds's avatar
hrchilds committed
438
    TimingsManager::Initialize(progname);
hrchilds's avatar
hrchilds committed
439 440
    // In case TimingsManager was already initialized...
    visitTimer->SetFilename(progname);
hrchilds's avatar
hrchilds committed
441 442
    if(enableTimings)
        visitTimer->Enable();
hrchilds's avatar
hrchilds committed
443 444

#if !defined(_WIN32)
445
    std::set_new_handler(NewHandler);
hrchilds's avatar
hrchilds committed
446 447 448 449 450 451 452 453 454 455 456 457 458
#endif

#if defined(_WIN32)
    // Windows specific code
    WORD wVersionRequested;
    WSADATA wsaData;

    // Initiate the use of a Winsock DLL (WS2_32.DLL), necessary for sockets.
    wVersionRequested = MAKEWORD(2,2);
    WSAStartup(wVersionRequested, &wsaData);
#endif
}

hrchilds's avatar
hrchilds committed
459
// ****************************************************************************
460
//  Function: VisItInit::SetComponentName
hrchilds's avatar
hrchilds committed
461 462 463 464 465 466
//
//  Purpose: Sets the name of the component
//
// ****************************************************************************

void
467
VisItInit::SetComponentName(const char *cname)
hrchilds's avatar
hrchilds committed
468
{
469
   size_t len;
hrchilds's avatar
hrchilds committed
470

hrchilds's avatar
hrchilds committed
471
   if (cname != 0 && (len = strlen(cname)) > 0)
hrchilds's avatar
hrchilds committed
472 473 474 475 476 477 478 479
   {
      len = len < sizeof(componentName) ? len : sizeof(componentName) - 1;
      strncpy(componentName, cname, len);
      componentName[len]='\0';
   }
}

// ****************************************************************************
480
//  Function: VisItInit::GetComponentName
hrchilds's avatar
hrchilds committed
481 482 483 484 485 486 487
//
//  Purpose: Gets the name of the component. Defaults to name of the
//  executable of it was not set.
//
// ****************************************************************************

const char *
488
VisItInit::GetComponentName(void)
hrchilds's avatar
hrchilds committed
489 490 491 492
{
   return (const char *) componentName;
}

hrchilds's avatar
hrchilds committed
493
// ****************************************************************************
494
//  Function: VisItInit::ComponentNameToID
hrchilds's avatar
hrchilds committed
495 496 497 498
//
//  Purpose: Return integer index of component name in list of all components
//
// ****************************************************************************
499
int
500
VisItInit::ComponentNameToID(const char *compName)
hrchilds's avatar
hrchilds committed
501 502 503 504 505 506 507 508 509 510 511
{
    int n = sizeof(allComponentNames) / sizeof(allComponentNames[0]);
    for (int i = 0; i < n; i++)
    {
        if (strcmp(compName, allComponentNames[i]) == 0)
            return i;
    }
    return -1;
}

// ****************************************************************************
512
//  Function: VisItInit::ComponentNameToID
hrchilds's avatar
hrchilds committed
513 514 515 516 517
//
//  Purpose: Return name of component with given index in list of all components
//
// ****************************************************************************
const char*
518
VisItInit::ComponentIDToName(const int id)
hrchilds's avatar
hrchilds committed
519 520 521 522 523 524 525 526 527
{
    int n = sizeof(allComponentNames) / sizeof(allComponentNames[0]);

    if (id >= 0 && id < n)
        return allComponentNames[id];
    else
        return "unknown";
}

hrchilds's avatar
hrchilds committed
528
// ****************************************************************************
529
//  Function: VisItInit::IsComponent
hrchilds's avatar
hrchilds committed
530 531 532 533 534
//
//  Purpose: Tests name of component against name passed as argument 
//
// ****************************************************************************
bool
535
VisItInit::IsComponent(const char *compName)
hrchilds's avatar
hrchilds committed
536
{
537
   if (strcmp(compName, VisItInit::GetComponentName()) == 0)
hrchilds's avatar
hrchilds committed
538 539 540 541 542
       return true;
   else
       return false;
}

hrchilds's avatar
hrchilds committed
543
// ****************************************************************************
544
//  Function: VisItInit::GetExecutableName
hrchilds's avatar
hrchilds committed
545 546 547 548 549 550
//
//  Purpose: Gets the name of the executable 
//
// ****************************************************************************

const char *
551
VisItInit::GetExecutableName(void)
hrchilds's avatar
hrchilds committed
552 553 554 555
{
   return (const char *) executableName;
}

hrchilds's avatar
hrchilds committed
556
// ****************************************************************************
557
//  Function: VisItInit::ComponentRegisterErrorFunction
hrchilds's avatar
hrchilds committed
558 559 560 561 562 563 564 565 566 567
//
//  Purpose:
//      Registers an error function.
//
//  Programmer: Hank Childs
//  Creation:   August 8, 2003
//
// ****************************************************************************

void
568
VisItInit::ComponentRegisterErrorFunction(ErrorFunction ef, void *args)
hrchilds's avatar
hrchilds committed
569 570 571 572 573 574 575
{
    errorFunctionArgs = args;
    errorFunction = ef;
}


// ****************************************************************************
576
//  Function: VisItInit::ComponentIssueError
hrchilds's avatar
hrchilds committed
577 578 579 580 581 582 583 584 585 586
//
//  Purpose:
//      Issues an error message on that component.
//
//  Programmer: Hank Childs
//  Creation:   August 8, 2003
//
// ****************************************************************************

void
587
VisItInit::ComponentIssueError(const char *msg)
hrchilds's avatar
hrchilds committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601
{
    debug1 << "Error issued: " << msg << endl;
    if (errorFunction != NULL)
    {
        errorFunction(errorFunctionArgs, msg);
    }
    else
    {
        debug1 << "Not able to issue error because no function was registered."
               << endl;
    }
}


hrchilds's avatar
hrchilds committed
602
// ****************************************************************************
603
// Method: VisItInit::Finalize
hrchilds's avatar
hrchilds committed
604 605 606 607 608 609 610 611
//
// Purpose: 
//   Calls cleanup functions before the application exits.
//
// Programmer: Brad Whitlock
// Creation:   Tue Mar 19 16:12:37 PST 2002
//
// Modifications:
hrchilds's avatar
hrchilds committed
612 613 614
//   Brad Whitlock, Wed Jun 18 11:14:50 PDT 2003
//   Added code to dump timings.
//
hrchilds's avatar
hrchilds committed
615 616 617
//   Hank Childs, Tue Jun  1 11:47:36 PDT 2004
//   Made the method be associated with the Init namespace.
//
hrchilds's avatar
hrchilds committed
618 619 620
//   Mark C. Miller, Tue Jul 25 18:26:10 PDT 2006
//   Pushed timer dump and finalization down into TimingsManager::Finalize 
//
621 622 623 624
//   Brad Whitlock, Wed Jun 25 10:44:24 PDT 2008
//   Added check to return early if Finalize has already been called, which
//   can happen when multiple VisIt components are part of the same executable.
//
hrchilds's avatar
hrchilds committed
625 626
// ****************************************************************************

hrchilds's avatar
hrchilds committed
627
void
628
VisItInit::Finalize(void)
hrchilds's avatar
hrchilds committed
629
{
630 631 632 633 634
    // Return if Finalize has already been called.
    if(finalizeCalled)
         return;
    finalizeCalled = true;

hrchilds's avatar
hrchilds committed
635
    TimingsManager::Finalize();
hrchilds's avatar
hrchilds committed
636

hrchilds's avatar
hrchilds committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
#if defined(_WIN32)
    // Terminates use of the WS2_32.DLL, the Winsock DLL.
    WSACleanup();
#endif
}

// ****************************************************************************
//  Function: RemovePrependedDirs
//
//  Purpose:
//      Removes prepended directories from a program name.
//
//  Arguments:
//      path      The input.  A program name with a path.
//      name      The output.  Only the name at the end.
//
//  Programmer:   Hank Childs
//  Creation:     August 13, 2001
//
656 657 658 659
//  Modifications:
//    Kathleen Bonnell, Wed Aug 22 18:00:57 PDT 2007
//    Use SLASH_CHAR.
//
hrchilds's avatar
hrchilds committed
660 661 662 663 664 665 666 667
// ****************************************************************************

static void
RemovePrependedDirs(const char *path, char *name)
{
    //
    // Find the last slash by going to the end and working backwards.
    //
bonnell's avatar
bonnell committed
668
    int  len = (int)strlen(path);
hrchilds's avatar
hrchilds committed
669
    int lastSlash;
camp's avatar
camp committed
670
    for (lastSlash=len ; lastSlash>=0 && path[lastSlash]!=visitcommon::SlashChar() ; lastSlash--)
hrchilds's avatar
hrchilds committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684
    {
        continue;
    }

    //
    // Last slash can get to be less than zero if there are no slashes.
    //
    if (lastSlash < 0)
    {
        lastSlash = 0;
    }

    //
    //
685
    if (path[lastSlash] == visitcommon::SlashChar())
hrchilds's avatar
hrchilds committed
686 687 688 689 690 691 692 693
    {
        strcpy(name, path + lastSlash + 1);
    }
    else
    {
        strcpy(name, path + lastSlash);
    }
}