
length string constructors. Print a developer warning if we have a too-long field (in developer builds only). This commit was SVN r21315.
216 строки
5.6 KiB
C++
216 строки
5.6 KiB
C++
//
|
|
// Copyright (c) 2004-2009 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 (c) 2009 Cisco Systems, Inc. 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"
|
|
|
|
#include "opal/util/show_help.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, bool strip_leading_whitespace,
|
|
bool strip_trailing_whitespace)
|
|
{
|
|
string local_value = 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 (strip_leading_whitespace) {
|
|
string::size_type i = 0;
|
|
while (i < local_value.length() && isspace(local_value[i])) {
|
|
++i;
|
|
}
|
|
if (i > local_value.length()) {
|
|
local_value = "";
|
|
} else if (i > 0) {
|
|
local_value = local_value.substr(i);
|
|
}
|
|
}
|
|
if (strip_trailing_whitespace) {
|
|
string::size_type i = local_value.length();
|
|
if (i > 0) {
|
|
// Note that string::size_type is unsigned, so we can't
|
|
// check for i<0
|
|
while (i > 0 && isspace(local_value[i])) {
|
|
--i;
|
|
}
|
|
// Instead, just check to see if we got down to 0 and the
|
|
// last character is also a space
|
|
if (i > 0) {
|
|
local_value = local_value.substr(0, i);
|
|
} else {
|
|
if (isspace(local_value[0])) {
|
|
local_value = "";
|
|
} else {
|
|
local_value = local_value[0];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pretty) {
|
|
string::size_type pos, max_value_width;
|
|
string spaces;
|
|
string v = local_value;
|
|
string filler;
|
|
|
|
int num_spaces = (int)(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 << ":" << local_value << endl;
|
|
} else {
|
|
cout << local_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) {
|
|
if (centerpoint > pretty_message.length()) {
|
|
string spaces(centerpoint - pretty_message.length(), ' ');
|
|
cout << spaces;
|
|
}
|
|
#if OPAL_ENABLE_DEBUG
|
|
else {
|
|
opal_show_help("help-ompi_info.txt",
|
|
"developer warning: field too long", false,
|
|
pretty_message.c_str(), centerpoint);
|
|
}
|
|
#endif
|
|
if (!pretty_message.empty()) {
|
|
cout << pretty_message << ": " << value << endl;
|
|
} else {
|
|
cout << " " << value << endl;
|
|
}
|
|
} else {
|
|
if (!plain_message.empty()) {
|
|
cout << plain_message << ":" << value << endl;
|
|
} else {
|
|
cout << value << endl;
|
|
}
|
|
}
|
|
}
|