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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 252 253 254 255 256 257 258 259 260 261 262
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <ornaments.h>
#include <string_lib.h>
#include <rlist.h>
#include <logging.h>
#include <fncall.h>
#include <promises.h> /* PromiseID */
/**
* @brief Like StringAppend(), but replace characters '*' and '#' with their visible counterparts.
* @param buffer Buffer to be used.
* @param src Constant string to append
* @param n Total size of dst buffer. The string will be truncated if this is exceeded.
*/
static bool StringAppendPromise(char *dst, const char *src, size_t n)
{
size_t i, j;
if (n == 0)
{
return false;
}
n--;
for (i = 0; i < n && dst[i]; i++)
{
}
for (j = 0; i < n && src[j]; i++, j++)
{
const char ch = src[j];
switch (ch)
{
case CF_MANGLED_NS:
dst[i] = ':';
break;
case CF_MANGLED_SCOPE:
dst[i] = '.';
break;
default:
dst[i] = ch;
break;
}
}
dst[i] = '\0';
return (i < n || !src[j]);
}
/**
* @brief Like @c BufferAppendPromiseStr, but if @c str contains newlines
* and is longer than 2*N+3, then only copy an abbreviated version
* consisting of the first and last N characters, separated by @c `...`
* @param buffer Buffer to be used.
* @param str Constant string to append
* @param n Total size of dst buffer. The string will be truncated if this is exceeded.
* @param max_fragment Max. length of initial/final segment of @c str to keep
* @note 2*max_fragment+3 is the maximum length of the appended string (excl. terminating NULL)
*
*/
static bool StringAppendAbbreviatedPromise(char *dst, const char *src, size_t n,
const size_t max_fragment)
{
/* check if `src` contains a new line (may happen for "insert_lines") */
const char *const nl = strchr(src, '\n');
if (nl == NULL)
{
return StringAppendPromise(dst, src, n);
}
else
{
/* `src` contains a newline: abbreviate it by taking the first and last few characters */
static const char sep[] = "...";
char abbr[sizeof(sep) + 2 * max_fragment];
const size_t head = (nl > src + max_fragment) ? max_fragment : (size_t) (nl - src);
const char * last_line = strrchr(src, '\n') + 1;
assert(last_line); /* not max_fragmentULL, we know we have at least one '\n' */
const size_t tail = strlen(last_line);
if (tail > max_fragment)
{
last_line += tail - max_fragment;
}
memcpy(abbr, src, head);
strcpy(abbr + head, sep);
strcat(abbr, last_line);
return StringAppendPromise(dst, abbr, n);
}
}
/*********************************************************************/
void SpecialTypeBanner(TypeSequence type, int pass)
{
if (type == TYPE_SEQUENCE_CONTEXTS)
{
Log(LOG_LEVEL_VERBOSE, "C: .........................................................");
Log(LOG_LEVEL_VERBOSE, "C: BEGIN classes / conditions (pass %d)", pass);
}
if (type == TYPE_SEQUENCE_VARS)
{
Log(LOG_LEVEL_VERBOSE, "V: .........................................................");
Log(LOG_LEVEL_VERBOSE, "V: BEGIN variables (pass %d)", pass);
}
}
void PromiseBanner(EvalContext *ctx, const Promise *pp)
{
char handle[CF_MAXVARSIZE];
const char *sp;
if ((sp = PromiseGetHandle(pp)) || (sp = PromiseID(pp)))
{
strlcpy(handle, sp, CF_MAXVARSIZE);
}
else
{
strcpy(handle, "");
}
Log(LOG_LEVEL_VERBOSE, "P: .........................................................");
if (strlen(handle) > 0)
{
Log(LOG_LEVEL_VERBOSE, "P: BEGIN promise '%s' of type \"%s\" (pass %d)", handle, PromiseGetPromiseType(pp), EvalContextGetPass(ctx));
}
else
{
Log(LOG_LEVEL_VERBOSE, "P: BEGIN un-named promise of type \"%s\" (pass %d)", PromiseGetPromiseType(pp), EvalContextGetPass(ctx));
}
const size_t n = 2*CF_MAXFRAGMENT + 3;
char pretty_promise_name[n+1];
pretty_promise_name[0] = '\0';
StringAppendAbbreviatedPromise(pretty_promise_name, pp->promiser, n, CF_MAXFRAGMENT);
Log(LOG_LEVEL_VERBOSE, "P: Promiser/affected object: '%s'", pretty_promise_name);
Rlist *params = NULL;
char *varclass;
FnCall *fp;
if ((params = EvalContextGetBundleArgs(ctx)))
{
Writer *w = StringWriter();
RlistWrite(w, params);
Log(LOG_LEVEL_VERBOSE, "P: From parameterized bundle: %s(%s)", PromiseGetBundle(pp)->name, StringWriterData(w));
WriterClose(w);
}
else
{
Log(LOG_LEVEL_VERBOSE, "P: Part of bundle: %s", PromiseGetBundle(pp)->name);
}
Log(LOG_LEVEL_VERBOSE, "P: Base context class: %s", pp->classes);
if ((varclass = PromiseGetConstraintAsRval(pp, "if", RVAL_TYPE_SCALAR)) || (varclass = PromiseGetConstraintAsRval(pp, "ifvarclass", RVAL_TYPE_SCALAR)))
{
Log(LOG_LEVEL_VERBOSE, "P: \"if\" class condition: %s", varclass);
}
else if ((fp = (FnCall *)PromiseGetConstraintAsRval(pp, "if", RVAL_TYPE_FNCALL)) || (fp = (FnCall *)PromiseGetConstraintAsRval(pp, "ifvarclass", RVAL_TYPE_FNCALL)))
{
Writer *w = StringWriter();
FnCallWrite(w, fp);
Log(LOG_LEVEL_VERBOSE, "P: \"if\" class condition: %s", StringWriterData(w));
}
else if ((varclass = PromiseGetConstraintAsRval(pp, "unless", RVAL_TYPE_SCALAR)))
{
Log(LOG_LEVEL_VERBOSE, "P: \"unless\" class condition: %s", varclass);
}
else if ((fp = (FnCall *)PromiseGetConstraintAsRval(pp, "unless", RVAL_TYPE_FNCALL)))
{
Writer *w = StringWriter();
FnCallWrite(w, fp);
Log(LOG_LEVEL_VERBOSE, "P: \"unless\" class condition: %s", StringWriterData(w));
}
Log(LOG_LEVEL_VERBOSE, "P: Stack path: %s", EvalContextStackToString(ctx));
if (pp->comment)
{
Log(LOG_LEVEL_VERBOSE, "P:\n");
Log(LOG_LEVEL_VERBOSE, "P: Comment: %s", pp->comment);
}
}
void Legend()
{
Log(LOG_LEVEL_VERBOSE, "----------------------------------------------------------------");
Log(LOG_LEVEL_VERBOSE, "PREFIX LEGEND:");
Log(LOG_LEVEL_VERBOSE, " V: variable or parameter new definition in scope");
Log(LOG_LEVEL_VERBOSE, " C: class/context new definition ");
Log(LOG_LEVEL_VERBOSE, " B: bundle start/end execution marker");
Log(LOG_LEVEL_VERBOSE, " P: promise execution output ");
Log(LOG_LEVEL_VERBOSE, " A: accounting output ");
Log(LOG_LEVEL_VERBOSE, " T: time measurement for stated object (promise or bundle)");
Log(LOG_LEVEL_VERBOSE, "----------------------------------------------------------------");
}
void Banner(const char *s)
{
Log(LOG_LEVEL_VERBOSE, "----------------------------------------------------------------");
Log(LOG_LEVEL_VERBOSE, " %s ", s);
Log(LOG_LEVEL_VERBOSE, "----------------------------------------------------------------");
}
void BundleBanner(const Bundle *bp, const Rlist *params)
{
Log(LOG_LEVEL_VERBOSE, "B: *****************************************************************");
if (params)
{
Writer *w = StringWriter();
RlistWrite(w, params);
Log(LOG_LEVEL_VERBOSE, "B: BEGIN bundle %s(%s)", bp->name, StringWriterData(w));
WriterClose(w);
}
else
{
Log(LOG_LEVEL_VERBOSE, "B: BEGIN bundle %s", bp->name);
}
Log(LOG_LEVEL_VERBOSE, "B: *****************************************************************");
}
void EndBundleBanner(const Bundle *bp)
{
if (bp == NULL)
{
return;
}
Log(LOG_LEVEL_VERBOSE, "B: *****************************************************************");
Log(LOG_LEVEL_VERBOSE, "B: END bundle %s", bp->name);
Log(LOG_LEVEL_VERBOSE, "B: *****************************************************************");
}
|