Print colored log of PVS-Studio analysis in Linux terminal - pvs-studio

I'm trying to integrate PVS-Studio analysis into CI for my homework. Everything seems to work fine except log printing; I want warnings to be colored or highlighted in some other way.
The best I could think of is to use plog-converter to output in html format and then use elinks -dump -dump-color-mode 1 to output that in terminal but it looks kinda weird.
Is there a better way to do it?

I think the best way is to modify the source of the plog-converter. The source code of the utility is published on GitHub so that users can expand the functionality for their tasks.

Since plog-converter can't do it out of the box and modifying its source code is a bit extreme, I decided to highlight output myself.
After a bit of a fiddling with syntax highlighting in terminal I found out that the simplest way is just using grep kinda like this:
plog-converter -t errorfile project.log | \
GREP_COLOR='01;31' grep -E --color=always 'error:|$' | \
GREP_COLOR='01;33' grep -E --color=always 'warning:|$'
I suppose errorfile format should only containt "errors" and "warning" so this colorizes just these two words with two different colors

Related

Colored grep causing trouble when run in subprocess (OS X)

I'm having troubles in doing simple stuff with grep in my Mac. I'd like to rename a file, and for that I use grep and sed.
If I don't use grep, it works fine.
If I use grep, it doesn't work: mv shows a weird error.
For what I understand, the reason may be the colored grep I am using, and then mv uses not the filename, but the code for displaying the colored filename in the console (as elm content is a colored yum.txt, instead of a normal one).
Is this the reason? What can I do?
You are right, those strange characters are indeed color codes. You can search for the --color= option either in GREP_OPTIONS environment variable (printenv) and/or your user defined aliases.
Then just change --color=always to --color=auto; with the --color=auto option set, grep should display color codes only when the standard output is connected to a terminal and plain text otherwise.

Why does a pipe to a command group only work for some commands?

I was interested in keeping the header of the input in the grep output just like in the question Include header in the 'grep' result.
Although a working solution (using sed) is presented right in the question, the answer with most upvotes attracted my attention. It allows utilizing grep --color, for example. It suggests piping the input to a command group with the commands head and grep, e.g.:
ps -ef | { head -1; grep python; }
However, as written in the linked answer and a comment, this does not work with all commands on the left side of the pipe. It works well with ps or lsof but does not work for df, ls and others. When using them, only the output of the first command in the group is printed:
$ df | { head -1; grep '/'; }
Filesystem 1K-blocks Used Available Use% Mounted on
$
Could anyone explain why piping to a command group only works for some commands? Is there any way to make it work universally?
The answer to your question is in comments of the linked answer.
Apparently ps -e is sending the header line first, then not sending anything, then buffering the rest of the output. head must think the stream is closed after the first line, so it exits, leaving grep to see the rest.
It only works by accident.
Is there any way to make it work universally?
Everything is possible, but, you may need to recode and recompile everything else. So... not possible, sorry.

How to color console output of a custom program

Problem:
I'm using Terminator but I think this question is relevant to any terminal.
To be more precise let me please explain my problem on concrete example.
I'm running Android cts tests, so I need to use cts-tradef script. This is how it looks like:
The script just runs jar (which I don't want to modify), however I would like to change color of cts-tf, so it looks like on a picture below:
My unsuccessful attempts to solve the problem:
For now I've tried doing something like this:
echo -e "\033[01;32m" && ./cts-tradefed
However it will color everything (as on below picture) while I want to color only cts-tf string (as above):
I also tried using sed, however although it works and replace some strings it also finishes cts-tradefed, so it's useless to me. Same thing (cts-tradefed finishes) happens when piping it through grep:
./cts-tradefed | grep --color "cts-tf\|$"
Another try was with grc tool mentioned by Anthony Geoghegan. Running it without config file doesn't do anything but cts-tradefed doesn't finish but when I run it with config file cts-tradefed finishes the same as with grep or sed. My config file was ok, since it works good with cat or similar commands.
I haven’t used it myself but Radovan Garabík’s Generic Colouriser looks like it should do what you want. It’s written in Python “for beautifying your logfiles or output of commands”.
Packages are available for Debian-based operating systems but I imagine it should not be too hard to install if you are familiar with Python.
GitHub repository

searching in the source code

Often I do different projects and sometimes there is a lack of documentation.
So I decided to use open-source code for looking how people solved different problems.
The idea is if I run into function what I don't how to use I look for different developers used that function before.
Approach:
I downloaded a few pretty decent projects done by other people and put them into one folder.
Now, if I don't know how a function is used (e.g. main() ), I do :
find . -name \*.py | xargs cat | grep -n "main()"
Consequently I get examples of its use:
But there is a problem. I don't know from which file examples are. It'd be perfectly if it was possible to get name of the file as well as number of line.
It seems to be limitation of use "cat" command because it mixes all files together and as result I get information about number not in the file but rather in cat output. So I feel this approach is bad in the root.
i.e.
I want to be able to look for functions/symbols in plethora of source code
and get information about the line and file where a certain combination was met.
I prefer console-way.
Any advice?
Try this:
find . -name \*.py -exec grep -nH "main()" {} \;
Explanation:
The "-exec" option says to execute the following command, up until \; for each file it finds.
The "-H" option to grep causes it to print the name of the file in which the string was found.
The "-n" option causes grep to print the line numbers.
The {} is a placeholder that expands to the name of the file that "find" just found.
You need only grep command:
$ grep -nr 'main()' /path/to/projects/folder/* | grep '.py:'
Want to search source files ? Why not http://beyondgrep.com/ ?
I wont answer you from the point of the bash.
I dont know which editor/IDE are you using, but for code dissecting there is no better tool for me then:
Vim with Ctags combination
Ctrl-p,Ctrl-p funky and MRU plugin +
proper search and regex usage.
good vim debugger
There is no part of code that cant be examined. Please sorry if are using some other tools, I am just suggesting you what do I find is the best for code analysis for me.

Possible to grab a text from a online .txt file via bash?

Is it possible to grab text from a online text file via grep/cat/awk or someting else? (in bash)
The way i currently do this is i download the text file to the drive and grep/cat into the file for it's text.
curl -o "$TMPDIR"/"text.txt" http://www.example.com/text.txt
cat/grep "$TMPDIR"/text.txt
rm -rf "$TMPDIR"/"text.txt"
Is one of the text grabbers (or another one) capable enough to grab something from a text file on the internet?
This would get rid of the whole downloadfile-readfile-deletefile process and just replace it with one command, speeding up things considerably if you have a lot of those strings.
I couldn't find anything via the man pages or googling around, maybe you guys know something.
Use curl -o - http://www.example.com/text.txt | grep "something".
-o - tells curl that it "downloads to stdout", other utils such as wget, lynx and links also have corresponding functionality.
You might try netcat - this is exactly what it was made for.
You could at least pipe your commands to avoid manually creating a temporary file:
curl … | cat/grep …

Resources