tools/perf/util/color.c
Source file repositories/reference/linux-study-clean/tools/perf/util/color.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/color.c- Extension
.c- Size
- 3395 bytes
- Lines
- 157
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
linux/kernel.hsubcmd/pager.hstdlib.hstdio.hstring.hcolor.hmath.hunistd.h
Detected Declarations
function __color_vsnprintffunction __color_vfprintffunction color_vsnprintffunction color_vfprintffunction color_snprintffunction color_fprintffunction percent_color_fprintffunction value_color_snprintffunction percent_color_snprintffunction percent_color_len_snprintf
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <linux/kernel.h>
#include <subcmd/pager.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "color.h"
#include <math.h>
#include <unistd.h>
int perf_use_color_default = -1;
static int __color_vsnprintf(char *bf, size_t size, const char *color,
const char *fmt, va_list args, const char *trail)
{
int r = 0;
/*
* Auto-detect:
*/
if (perf_use_color_default < 0) {
if (isatty(1) || pager_in_use())
perf_use_color_default = 1;
else
perf_use_color_default = 0;
}
if (perf_use_color_default && *color)
r += scnprintf(bf, size, "%s", color);
r += vscnprintf(bf + r, size - r, fmt, args);
if (perf_use_color_default && *color)
r += scnprintf(bf + r, size - r, "%s", PERF_COLOR_RESET);
if (trail)
r += scnprintf(bf + r, size - r, "%s", trail);
return r;
}
/* Colors are not included in return value */
static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
va_list args)
{
int r = 0;
/*
* Auto-detect:
*/
if (perf_use_color_default < 0) {
if (isatty(fileno(fp)) || pager_in_use())
perf_use_color_default = 1;
else
perf_use_color_default = 0;
}
if (perf_use_color_default && *color)
fprintf(fp, "%s", color);
r += vfprintf(fp, fmt, args);
if (perf_use_color_default && *color)
fprintf(fp, "%s", PERF_COLOR_RESET);
return r;
}
int color_vsnprintf(char *bf, size_t size, const char *color,
const char *fmt, va_list args)
{
return __color_vsnprintf(bf, size, color, fmt, args, NULL);
}
int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args)
{
return __color_vfprintf(fp, color, fmt, args);
}
int color_snprintf(char *bf, size_t size, const char *color,
const char *fmt, ...)
{
va_list args;
int r;
va_start(args, fmt);
r = color_vsnprintf(bf, size, color, fmt, args);
va_end(args);
return r;
}
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
{
va_list args;
int r;
va_start(args, fmt);
Annotation
- Immediate include surface: `linux/kernel.h`, `subcmd/pager.h`, `stdlib.h`, `stdio.h`, `string.h`, `color.h`, `math.h`, `unistd.h`.
- Detected declarations: `function __color_vsnprintf`, `function __color_vfprintf`, `function color_vsnprintf`, `function color_vfprintf`, `function color_snprintf`, `function color_fprintf`, `function percent_color_fprintf`, `function value_color_snprintf`, `function percent_color_snprintf`, `function percent_color_len_snprintf`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.