Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:47:27

0001 /*
0002  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
0003  *                         University Research and Technology
0004  *                         Corporation.  All rights reserved.
0005  * Copyright (c) 2004-2006 The University of Tennessee and The University
0006  *                         of Tennessee Research Foundation.  All rights
0007  *                         reserved.
0008  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
0009  *                         University of Stuttgart.  All rights reserved.
0010  * Copyright (c) 2004-2005 The Regents of the University of California.
0011  *                         All rights reserved.
0012  * Copyright (c) 2007-2011 Cisco Systems, Inc.  All rights reserved.
0013  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
0014  * Copyright (c) 2015-2020 Intel, Inc.  All rights reserved.
0015  * Copyright (c) 2016      Research Organization for Information Science
0016  *                         and Technology (RIST). All rights reserved.
0017  * Copyright (c) 2021-2022 Nanook Consulting.  All rights reserved.
0018  * $COPYRIGHT$
0019  *
0020  * Additional copyrights may follow
0021  *
0022  * $HEADER$
0023  */
0024 
0025 /** @file
0026  * PMIX output stream facility.
0027  *
0028  * The PMIX output stream facility is used to send output from the PMIX
0029  * libraries to output devices.  It is meant to fully replace all
0030  * forms of printf() (and friends).  Output streams are opened via the
0031  * pmix_output_open() function call, and then sent output via
0032  * pmix_output_verbose(), PMIX_OUTPUT(), and pmix_output().  Streams are
0033  * closed with pmix_output_close().
0034  *
0035  * Streams can multiplex output to several kinds of outputs (one of
0036  * each):
0037  *
0038  * - the syslog (if available)
0039  * - standard output
0040  * - standard error
0041  * - file
0042  *
0043  * Which outputs to use are specified during pmix_output_open().
0044  *
0045  * WARNING: When using "file" as an output destination, be aware that
0046  * the file may not exist until the session directory for the process
0047  * exists.  This is at least part of the way through MPI_INIT (for
0048  * example).  Most MCA components and internals of PMIx won't be
0049  * affected by this, but some RTE / startup aspects of PMIx will
0050  * not be able to write to a file for output.  See pmix_output() for
0051  * details on what happens in these cases.
0052  *
0053  * pmix_output_open() returns an integer handle that is used in
0054  * successive calls to PMIX_OUTPUT() and pmix_output() to send output to
0055  * the stream.
0056  *
0057  * The default "verbose" stream is opened after invoking
0058  * pmix_output_init() (and closed after invoking
0059  * pmix_output_finalize()).  This stream outputs to stderr only, and
0060  * has a stream handle ID of 0.
0061  *
0062  * It is erroneous to have one thread close a stream and have another
0063  * try to write to it.  Multiple threads writing to a single stream
0064  * will be serialized in an unspecified order.
0065  */
0066 
0067 #ifndef PMIX_OUTPUT_H_
0068 #define PMIX_OUTPUT_H_
0069 
0070 #include "src/include/pmix_config.h"
0071 
0072 #ifdef HAVE_STDARG_H
0073 #    include <stdarg.h>
0074 #endif
0075 
0076 #include "src/class/pmix_object.h"
0077 
0078 BEGIN_C_DECLS
0079 
0080 #define PMIX_OUTPUT_MAX_STREAMS 64
0081 
0082 /* There are systems where all output needs to be redirected to syslog
0083  * and away from stdout/stderr or files - e.g., embedded systems whose
0084  * sole file system is in flash. To support such systems, we provide
0085  * the following environmental variables that support redirecting -all-
0086  * output (both from pmix_output and stdout/stderr of processes) to
0087  * syslog:
0088  *
0089  * PMIX_OUTPUT_REDIRECT - set to "syslog" to redirect to syslog. Other
0090  *                        options may someday be supported
0091  * PMIX_OUTPUT_SYSLOG_PRI - set to "info", "error", or "warn" to have
0092  *                        output sent to syslog at that priority
0093  * PMIX_OUTPUT_SYSLOG_IDENT - a string identifier for the log
0094  *
0095  * We also define two global variables that notify all other
0096  * layers that output is being redirected to syslog at the given
0097  * priority. These are used, for example, by the IO forwarding
0098  * subsystem to tell it to dump any collected output directly to
0099  * syslog instead of forwarding it to another location.
0100  */
0101 PMIX_EXPORT extern bool pmix_output_redirected_to_syslog;
0102 PMIX_EXPORT extern int pmix_output_redirected_syslog_pri;
0103 
0104 /**
0105  * \class pmix_output_stream_t
0106  *
0107  * Structure used to request the opening of a PMIX output stream.  A
0108  * pointer to this structure is passed to pmix_output_open() to tell
0109  * the pmix_output subsystem where to send output for a given stream.
0110  * It is valid to specify multiple destinations of output for a stream
0111  * -- output streams can be multiplexed to multiple different
0112  * destinations through the pmix_output facility.
0113  *
0114  * Note that all strings in this struct are cached on the stream by
0115  * value; there is no need to keep them allocated after the return
0116  * from pmix_output_open().
0117  */
0118 struct pmix_output_stream_t {
0119     /** Class parent */
0120     pmix_object_t super;
0121 
0122     /**
0123      * Indicate the starting verbosity level of the stream.
0124      *
0125      * Verbose levels are a convenience mechanisms, and are only
0126      * consulted when output is sent to a stream through the
0127      * pmix_output_verbose() function.  Verbose levels are ignored in
0128      * PMIX_OUTPUT() and pmix_output().
0129      *
0130      * Valid verbose levels typically start at 0 (meaning "minimal
0131      * information").  Higher verbosity levels generally indicate that
0132      * more output and diagnostics should be displayed.
0133      */
0134     int lds_verbose_level;
0135 
0136     /**
0137      * When pmix_output_stream_t::lds_want_syslog is true, this field is
0138      * examined to see what priority output from the stream should be
0139      * sent to the syslog.
0140      *
0141      * This value should be set as per the syslog(3) man page.  It is
0142      * typically the OR value of "facilty" and "level" values described
0143      * in the man page.
0144      */
0145     int lds_syslog_priority;
0146     /**
0147      * When pmix_output_stream_t::lds_want_syslog is true, this field is
0148      * examined to see what ident value should be passed to openlog(3).
0149      *
0150      * If a NULL value is given, the string "pmix" is used.
0151      */
0152 #if !defined(__WINDOWS__)
0153     char *lds_syslog_ident;
0154 #elif !defined(_MSC_VER)
0155     char *lds_syslog_ident;
0156 #else
0157     HANDLE lds_syslog_ident;
0158 #endif /* !defined(__WINDOWS__) */
0159 
0160     /**
0161      * String prefix added to all output on the stream.
0162      *
0163      * When this field is non-NULL, it is prefixed to all lines of
0164      * output on the stream.  When this field is NULL, no prefix is
0165      * added to each line of output in the stream. The prefix is copied
0166      * to an internal structure in the call to pmix_output_open()!
0167      */
0168     char *lds_prefix;
0169 
0170     /**
0171      * String suffix added to all output on the stream.
0172      *
0173      * When this field is non-NULL, it is appended to all lines of
0174      * output on the stream.  When this field is NULL, no suffix is
0175      * added to each line of output in the stream. The suffix is copied
0176      * to an internal structure in the call to pmix_output_open()!
0177      */
0178     char *lds_suffix;
0179 
0180     /**
0181      * Indicates whether the output of the stream is
0182      * debugging/developer-only output or not.
0183      *
0184      * This field should be "true" if the output is for debugging
0185      * purposes only.  In that case, the output will never be sent to
0186      * the stream unless PMIX was configured with --enable-debug.
0187      */
0188     bool lds_is_debugging;
0189 
0190     /**
0191      * Indicates whether output of the stream should be sent to the
0192      * syslog or not.
0193      *
0194      * If this field is true, output from this stream is sent to the
0195      * syslog, and the following fields are also examined:
0196      *
0197      * - lds_syslog_priority
0198      * - lds_syslog_ident
0199      * - lds_prefix
0200      *
0201      * If this field is false, the above three fields are ignored.
0202      */
0203     bool lds_want_syslog;
0204 
0205     /**
0206      * Whether to send stream output to stdout or not.
0207      *
0208      * If this field is true, stream output is sent to stdout.
0209      */
0210     bool lds_want_stdout;
0211     /**
0212      * Whether to send stream output to stderr or not.
0213      *
0214      * If this field is true, stream output is sent to stderr.
0215      */
0216     bool lds_want_stderr;
0217 
0218     /**
0219      * Whether to send stream output to a file or not.
0220      *
0221      * When this field is true, stream output is sent to a file, and the
0222      * following fields are also examined:
0223      *
0224      * - lds_want_file_append
0225      * - lda_file_suffix
0226      */
0227     bool lds_want_file;
0228     /**
0229      * When pmix_output_stream_t::lds_want_file is true, this field
0230      * indicates whether to append the file (if it exists) or overwrite
0231      * it.
0232      *
0233      * If false, the file is opened with the O_TRUNC flag.
0234      */
0235     bool lds_want_file_append;
0236     /**
0237      * When pmix_output_stream_t::lds_want_file is true, this field
0238      * indicates the string suffix to add to the filename.
0239      *
0240      * The output file will be in the directory and begin with the
0241      * prefix set by pmix_output_set_output_file_info() (e.g.,
0242      * "$dir/$prefix$suffix").  If this field is NULL and
0243      * lds_want_file is true, then the suffix "output.txt" is used.
0244      *
0245      * Note that it is possible that the output directory may not
0246      * exist when pmix_output_open() is invoked.  See pmix_output()
0247      * for details on what happens in this situation.
0248      */
0249     char *lds_file_suffix;
0250 };
0251 
0252 /**
0253  * Convenience typedef
0254  */
0255 typedef struct pmix_output_stream_t pmix_output_stream_t;
0256 
0257 /* tracking structure */
0258 typedef struct {
0259     bool ldi_used;
0260     bool ldi_enabled;
0261     int ldi_verbose_level;
0262 
0263     bool ldi_syslog;
0264     int ldi_syslog_priority;
0265 
0266     char *ldi_syslog_ident;
0267     char *ldi_prefix;
0268     int ldi_prefix_len;
0269 
0270     char *ldi_suffix;
0271     int ldi_suffix_len;
0272 
0273     bool ldi_stdout;
0274     bool ldi_stderr;
0275 
0276     bool ldi_file;
0277     bool ldi_file_want_append;
0278     char *ldi_file_suffix;
0279     int ldi_fd;
0280     int ldi_file_num_lines_lost;
0281 } pmix_output_desc_t;
0282 
0283 PMIX_EXPORT extern pmix_output_desc_t pmix_output_info[];
0284 
0285 /**
0286  * Initializes the output stream system and opens a default
0287  * "verbose" stream.
0288  *
0289  * @retval true Upon success.
0290  * @retval false Upon failure.
0291  *
0292  * This should be the first function invoked in the output
0293  * subsystem.  After this call, the default "verbose" stream is open
0294  * and can be written to via calls to pmix_output_verbose() and
0295  * pmix_output_error().
0296  *
0297  * By definition, the default verbose stream has a handle ID of 0,
0298  * and has a verbose level of 0.
0299  */
0300 PMIX_EXPORT bool pmix_output_init(void);
0301 
0302 /**
0303  * Shut down the output stream system.
0304  *
0305  * Shut down the output stream system, including the default verbose
0306  * stream.
0307  */
0308 PMIX_EXPORT void pmix_output_finalize(void);
0309 
0310 /**
0311  * Opens an output stream.
0312  *
0313  * @param lds A pointer to pmix_output_stream_t describing what the
0314  * characteristics of the output stream should be.
0315  *
0316  * This function opens an output stream and returns an integer
0317  * handle.  The caller is responsible for maintaining the handle and
0318  * using it in successive calls to PMIX_OUTPUT(), pmix_output(),
0319  * pmix_output_switch(), and pmix_output_close().
0320  *
0321  * If lds is NULL, the default descriptions will be used, meaning
0322  * that output will only be sent to stderr.
0323  *
0324  * It is safe to have multiple threads invoke this function
0325  * simultaneously; their execution will be serialized in an
0326  * unspecified manner.
0327  *
0328  * Be sure to see pmix_output() for a description of what happens
0329  * when open_open() / pmix_output() is directed to send output to a
0330  * file but the process session directory does not yet exist.
0331  */
0332 PMIX_EXPORT int pmix_output_open(pmix_output_stream_t *lds);
0333 
0334 /**
0335  * Re-opens / redirects an output stream.
0336  *
0337  * @param output_id Stream handle to reopen
0338  * @param lds A pointer to pmix_output_stream_t describing what the
0339  * characteristics of the reopened output stream should be.
0340  *
0341  * This function redirects an existing stream into a new [set of]
0342  * location[s], as specified by the lds parameter.  If the output_id
0343  * passed is invalid, this call is effectively the same as opening a
0344  * new stream with a specific stream handle.
0345  */
0346 PMIX_EXPORT int pmix_output_reopen(int output_id, pmix_output_stream_t *lds);
0347 
0348 /**
0349  * Enables and disables output streams.
0350  *
0351  * @param output_id Stream handle to switch
0352  * @param enable Boolean indicating whether to enable the stream
0353  * output or not.
0354  *
0355  * @returns The previous enable state of the stream (true == enabled,
0356  * false == disabled).
0357  *
0358  * The output of a stream can be temporarily disabled by passing an
0359  * enable value to false, and later resumed by passing an enable
0360  * value of true.  This does not close the stream -- it simply tells
0361  * the pmix_output subsystem to intercept and discard any output sent
0362  * to the stream via PMIX_OUTPUT() or pmix_output() until the output
0363  * is re-enabled.
0364  */
0365 PMIX_EXPORT bool pmix_output_switch(int output_id, bool enable);
0366 
0367 /**
0368  * \internal
0369  *
0370  * Reopens all existing output streams.
0371  *
0372  * This function should never be called by user applications; it is
0373  * typically only invoked after a restart (i.e., in a new process)
0374  * where output streams need to be re-initialized.
0375  */
0376 PMIX_EXPORT void pmix_output_reopen_all(void);
0377 
0378 /**
0379  * Close an output stream.
0380  *
0381  * @param output_id Handle of the stream to close.
0382  *
0383  * Close an output stream.  No output will be sent to the stream
0384  * after it is closed.  Be aware that pmix_output_handles tend to be
0385  * re-used; it is possible that after a stream is closed, if another
0386  * stream is opened, it will get the same handle value.
0387  */
0388 PMIX_EXPORT void pmix_output_close(int output_id);
0389 
0390 /**
0391  * Main function to send output to a stream.
0392  *
0393  * @param output_id Stream id returned from pmix_output_open().
0394  * @param format printf-style format string.
0395  * @param varargs printf-style varargs list to fill the string
0396  * specified by the format parameter.
0397  *
0398  * This is the main function to send output to custom streams (note
0399  * that output to the default "verbose" stream is handled through
0400  * pmix_output_verbose() and pmix_output_error()).
0401  *
0402  * It is never necessary to send a trailing "\n" in the strings to
0403  * this function; some streams requires newlines, others do not --
0404  * this function will append newlines as necessary.
0405  *
0406  * Verbosity levels are ignored in this function.
0407  *
0408  * Note that for output streams that are directed to files, the
0409  * files are stored under the process' session directory.  If the
0410  * session directory does not exist when pmix_output() is invoked,
0411  * the output will be discarded!  Once the session directory is
0412  * created, pmix_output() will automatically create the file and
0413  * writing to it.
0414  */
0415 PMIX_EXPORT void pmix_output(int output_id, const char *format, ...)
0416     __pmix_attribute_format__(__printf__, 2, 3);
0417 
0418 /**
0419  * Send output to a stream only if the passed verbosity level is
0420  * high enough.
0421  *
0422  * @param output_id Stream id returned from pmix_output_open().
0423  * @param level Target verbosity level.
0424  * @param format printf-style format string.
0425  * @param varargs printf-style varargs list to fill the string
0426  * specified by the format parameter.
0427  *
0428  * Output is only sent to the stream if the current verbosity level
0429  * is greater than or equal to the level parameter.  This mechanism
0430  * can be used to send "information" kinds of output to user
0431  * applications, but only when the user has asked for a high enough
0432  * verbosity level.
0433  *
0434  * It is never necessary to send a trailing "\n" in the strings to
0435  * this function; some streams requires newlines, others do not --
0436  * this function will append newlines as necessary.
0437  *
0438  * This function is really a convenience wrapper around checking the
0439  * current verbosity level set on the stream, and if the passed
0440  * level is less than or equal to the stream's verbosity level, this
0441  * function will effectively invoke pmix_output to send the output to
0442  * the stream.
0443  *
0444  * @see pmix_output_set_verbosity()
0445  */
0446 #define pmix_output_verbose(level, output_id, ...)              \
0447     if (output_id >= 0 && output_id < PMIX_OUTPUT_MAX_STREAMS &&    \
0448         pmix_output_info[output_id].ldi_verbose_level >= level) {   \
0449         pmix_output(output_id, __VA_ARGS__);                        \
0450     }
0451 
0452 /* protect legacy users */
0453 #define PMIX_OUTPUT_VERBOSE(a) pmix_output_verbose a
0454 #define PMIX_OUTPUT(a) pmix_output a
0455 
0456 /**
0457  * Set the verbosity level for a stream.
0458  *
0459  * @param output_id Stream id returned from pmix_output_open().
0460  * @param level New verbosity level
0461  *
0462  * This function sets the verbosity level on a given stream.  It
0463  * will be used for all future invocations of pmix_output_verbose().
0464  */
0465 PMIX_EXPORT void pmix_output_set_verbosity(int output_id, int level);
0466 
0467 /**
0468  * Get the verbosity level for a stream
0469  *
0470  * @param output_id Stream id returned from pmix_output_open()
0471  * @returns Verbosity of stream
0472  */
0473 PMIX_EXPORT int pmix_output_get_verbosity(int output_id);
0474 
0475 /**
0476  * Set characteristics for output files.
0477  *
0478  * @param dir Directory where output files will go
0479  * @param olddir If non-NULL, the directory where output files
0480  * were previously opened
0481  * @param prefix Prefix of files in the output directory
0482  * @param oldprefix If non-NULL, the old prefix
0483  *
0484  * This function controls the final filename used for all new
0485  * output streams that request output files.  Specifically, when
0486  * pmix_output_stream_t::lds_want_file is true, the output
0487  * filename will be of the form $dir/$prefix$suffix.
0488  *
0489  * The default value for the output directory is whatever is
0490  * specified in the TMPDIR environment variable if it exists, or
0491  * $HOME if it does not.  The default value for the prefix is
0492  * "output-pid<pid>-" (where "<pid>" is replaced by the PID of the
0493  * current process).
0494  *
0495  * If dir or prefix are NULL, new values are not set.  The strings
0496  * represented by dir and prefix are copied into internal storage;
0497  * it is safe to pass string constants or free() these values
0498  * after pmix_output_set_output_file_info() returns.
0499  *
0500  * If olddir or oldprefix are not NULL, copies of the old
0501  * directory and prefix (respectively) are returned in these
0502  * parameters.  The caller is responsible for calling (free) on
0503  * these values.  This allows one to get the old values, output an
0504  * output file in a specific directory and/or with a specific
0505  * prefix, and then restore the old values.
0506  *
0507  * Note that this function only affects the creation of \em new
0508  * streams -- streams that have already started writing to output
0509  * files are not affected (i.e., their output files are not moved
0510  * to the new directory).  More specifically, the pmix_output
0511  * system only opens/creates output files lazily -- so calling
0512  * this function affects both new streams \em and any stream that
0513  * was previously opened but had not yet output anything.
0514  */
0515 PMIX_EXPORT void pmix_output_set_output_file_info(const char *dir, const char *prefix,
0516                                                   char **olddir, char **oldprefix);
0517 
0518 /**
0519  * Same as pmix_output_verbose(), but pointer to buffer and size.
0520  */
0521 PMIX_EXPORT void pmix_output_hexdump(int verbose_level, int output_id, void *ptr, int buflen);
0522 
0523 /**
0524  * Declare the class of this type.  Note that the constructor for
0525  * this class is for convenience only -- it is \em not necessary
0526  * to be invoked.  If the constructor it used, it sets all values
0527  * in the struct to be false / 0 (i.e., turning off all output).
0528  * The intended usage is to invoke the constructor and then enable
0529  * the output fields that you want.
0530  */
0531 PMIX_EXPORT PMIX_CLASS_DECLARATION(pmix_output_stream_t);
0532 
0533 END_C_DECLS
0534 
0535 #endif /* PMIX_OUTPUT_H_ */