File: output.cc

package info (click to toggle)
openmpi 1.2.7~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 41,300 kB
  • ctags: 24,303
  • sloc: ansic: 224,835; sh: 22,627; makefile: 7,037; cpp: 6,353; asm: 3,547; lex: 528; objc: 383; perl: 348; csh: 89; f90: 49; fortran: 47; tcl: 12
file content (166 lines) | stat: -rw-r--r-- 4,170 bytes parent folder | download
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
//
// Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
//                         University Research and Technology
//                         Corporation.  All rights reserved.
// Copyright (c) 2004-2006 The University of Tennessee and The University
//                         of Tennessee Research Foundation.  All rights
//                         reserved.
// Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
//                         University of Stuttgart.  All rights reserved.
// Copyright (c) 2004-2005 The Regents of the University of California.
//                         All rights reserved.
// $COPYRIGHT$
// 
// Additional copyrights may follow
// 
// $HEADER$
//

#include "ompi_config.h"

#include <iostream>
#include <string>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef __WINDOWS__
#include <io.h>
#endif  /* __WINDOWS__ */

#include "ompi/tools/ompi_info/ompi_info.h"

using namespace std;
using namespace ompi_info;

#define OMPI_max(a,b) (((a) > (b)) ? (a) : (b)) 


//
// Private variables - set some reasonable screen size defaults
//

static int centerpoint = 24;
static int screen_width = 78;

// 
// Prints the passed strings in a pretty or parsable format.
//
void ompi_info::out(const string& pretty_message, const string &plain_message,
                    const string& value)
{
#ifdef HAVE_ISATTY
  // If we have isatty(), if this is not a tty, then disable
  // wrapping for grep-friendly behavior
  if (0 == isatty(STDOUT_FILENO)) {
      screen_width = INT_MAX;
  }
#endif

#ifdef TIOCGWINSZ
  if (screen_width < INT_MAX) {
      struct winsize size;
      if (ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*) &size) >= 0) {
          screen_width = size.ws_col;
      }
  }
#endif

  if (pretty) {
    string::size_type pos, max_value_width;
    string spaces;
    string v = value;
    string filler;

    int num_spaces = centerpoint - pretty_message.length();
    if (num_spaces > 0) {
      spaces = string(num_spaces, ' ');
    }

    max_value_width = screen_width - spaces.length() -
      pretty_message.length() - 2;
    if (!pretty_message.empty()) {
        filler = spaces + pretty_message + ": ";
    } else {
        filler = spaces + "  ";
    }

    while (true) {
      if (v.length() < max_value_width) {
        cout << filler << v << endl;
        break;
      } else {
        string spaces(centerpoint + 2, ' ');

        // Work backwards to find the first space before
        // max_value_width

        pos = v.rfind(' ', max_value_width);
        if (string::npos == pos) {

          // No space found < max_value_width.  Look for the first
          // space after max_value_width.

          pos = v.find(' ', max_value_width);

        if (string::npos == pos) {

            // There's just no spaces.  So just print it and be done.

            cout << filler << v << endl;
            break;
          } else {
            cout << filler << v.substr(0, pos) << endl;
            v = v.substr(pos + 1);
          }
        } else {
          cout << filler << v.substr(0, pos) << endl;
          v = v.substr(pos + 1);
        }

        // Reset for the next iteration

        filler = spaces;
      }
    }
  } else {
      if (!plain_message.empty()) {
          cout << plain_message << ":" << value << endl;
      } else {
          cout << value << endl;
      }
  }
}


// 
// Prints the passed integer in a pretty or parsable format.
//
void ompi_info::out(const string& pretty_message, const string &plain_message, 
                    int value)
{
  if (ompi_info::pretty) {
    string spaces(OMPI_max(centerpoint - pretty_message.length(), 0), ' ');
    if (!pretty_message.empty()) {
        cout << spaces << pretty_message << ": " << value << endl;
    } else {
        cout << spaces << "  " << value << endl;
    }
  } else {
      if (!plain_message.empty()) {
          cout << plain_message << ":" << value << endl;
      } else {
          cout << value << endl;
      }
  }
}