logging last eight character of the thread id? - c++

In my multithreaded program, I am logging process id and thread id. Thread id is generated using pthread_self() function. when I print in hex, generally it has 12 characters out of which first four are always common and I don't want to log them.
What is the best way in C++ to print last 8 characters, I tried to use boost::format but couldn't figure out how to print last 8 characters.
e.g
Here printing process id in decimal and thread id in hex.
std::cout << boost::format("%5d|%8X") %getpid() %pthread_self()
I have made it work with below solution but I don't like the magic number 10000000000
std::cout << boost::format("%5d|%8X") %getpid() %(pthread_self()%10000000000)
NOTE: For simplicity, here I used std::cout. In my application, I use boost::log so if there is a way to do using boost log, that's preferred.
Here is an example: 7f08 is common and doesn't add any value in debugging.
14833|7f084b7fe700
14833|7f084bfff700
14833|7f0850dec700
14833|7f08515ed700
14833|7f0851dee700
14833|7f08525ef700
14833|7f0852df0700
14833|7f08535f1700

Your magic constant should be in hex:
pthread_self() & 0xffffffff
This lets bits in the bottom 8 hex digits pass through unchanged, but sets bits in the higher hex digits to 0. They should then be suppressed, since you haven't specified leading 0 printing.

1) declare a char[13] variable on stack
2) sprintf there your thread id in %x form
3) then if you take a pointer to 4-th element of the array it will be your 8-char string - you can log it via whatever you like

Put the computation in a new function properly named, like last_part_of_thread_id(). Then the code will not look that magic inside such a function.

Related

In MS Visual Studio watch window, how to format a function-returned int as 4 char values?

If I put an int into Visual Studio's watch window, I can use various format additions to display it in different ways. So if I have "int myVariable = 10;" in the code, I can put "myVariable,x" into the watch window to have it display as hex. Likewise, "myVariable,c" makes it take the low byte and display it as a character. All the format tricks are documented here.
Now, with an int, I can do this:
(char*)(&myVariable),4
The ",4" says to display 4 values of the array. That will let me see the integer as a string of 4 characters -- this is some old software I'm working with that has a bunch of 4-character encodings. But I can't do that trick with a value returned from a function:
(char*)(&foo()),4
because I cannot take the address of the returned function. Watch window reports an error.
Does anyone know a trick where I can put a function into the watch window and format its integer return value as a string of characters?

C++ comparing two ints, error or wierd end

i use C++ in CODE:BLOCKS and ive made a little game with login, so when i wanna compare a variable(int) that came from scanf and a variable that i made, that wont work. So in the program i log in with a name and a pw, that works, than i get an exercise like: 65+34 and if i type ANY numbers, it will crash, if i enter a letter, it will say "A valasz rossz" that means my answer is incorrect, sorry for the hungarian program... translates:
A valasz helyes - answer is right
A valasz rossz - answer is wrong
Jatek! Valaszolj a helyesen- Game!Answer correctly
Bejelentkezes! - Login
Felhasznalonev - Nickname
Jelszo - pawssword
On meg nem regisztralt! 1 Regisztracio 2 Kilepes - U havent
registered yet! 1 register 2 Exit
So i think the problem is at the criterion if(ans==eo)
CODE:http://pastebin.com/E0AbRx8y HELP AT 104. LINE
Thanks for helping, it would be a lot for me! <3
In line 103, you need to pass scanf the ADDRESS of the variable where you want to store the value, so instead of:
scanf("%d",ans);
You should do:
scanf("%d", &ans);
You probably need to review all your calls to scanf and:
Ensure that you pass a pointer to the output variable
Check the return value in order to verify that you've read successfully the user's input (e.g. detect if you expect a number and the user enters a word)
Use C strings (arrays of char, which work with the printf and scanf functions) instead of C++ strings (std::string, which work with the >> and << operators)
Hope that it helps.
The problem is here :
scanf("%d",ans);
'scanf()' stores values, so it needs a place to store them.
This is done by providing the addresses (in pointers) of
where to store the values.
So write:
scanf("%d",&ans);
It will solve your problem.

C++ Primer 1.4.4 — Importance of EOF and how to write in a code that will end without EOF?

Referring to two questions:
Incorrect output from C++ Primer 1.4.4
Confused by control flow execution in C++ Primer example
My question is answered in both of those posts, but I want to delve further.
First, I know this is only the beginning, but let's say I make a fully functional program that runs in a designed window. By that level, will I already know how to implement a EOF? I can't expect someone running my program to know that they need to hit Control-Z.
Is there a way to implement a specific code that functions so that it does not need me to type in an unrecognized value?
Also one guy in those questions somewhat answered the importance of EOF, but how come the program doesn't even post the final cnt - 1?
Let's say I do the numbers 10 10 10 20 20 20. Without EOF, this will only show the "10 repeats 3 times." How come the program doesn't at least type in the count "10 repeats 3 times and 20 repeats 2 times" minus the final one with white space?
lets say I make a fully functional program that runs in a designed window. By that level, will I already know how to implement a eof? I can't expect someone running my program to know that they need to hit ctrl + z.
You could either tell the user explicitly to do a specific action to end input or the design of the window itself could tell the user the information implicitly. For instance, a dialog box could ask the user to enter input and click an OK button when done.
Is there a way to implement a specific code that functions so that it does not need me to type in an unrecognized value?
It seems like you would rather use a newline character to terminate your input. An example of this usage could be std::getline. Instead of writing
while (std::cin >> val)
you could instead use
std::string line;
if (std::getline(std::cin,line))
and assume that your user's input only consists of one line of values. There are plenty of other ways to similarly achieve this task depending on how you want to constrain the user's input.
Let's say I do the numbers 10 10 10 20 20 20. WIthout eof this will only show the "10 repeats 3 times." How come the program doesn't at least type in the count "10 repeats 3 times and 20 repeats 2 times" minus the final one with white space?
Without the eof your program is still executing the while (std::cin >> val) loop since std::cin >> val has not yet received invalid input.
Since the line
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
occurs after that while loop finishes execution, you don't (yet) see any information about the three 20's in the input.
When you are reading a sequence of inputs you'll need some indication when your down. That could be a sentinel value ("enter 999 to stop reading"; you'd need to detect that while reading), an invalid input ("enter X to stop reading"; when reading an int the value X is illegal and causes the stream to got into failure mode, i.e., have std::ios_base::failbit set), or the more conventional "there isn't anything more to read". For a file, the last conditions is straight forward. When reading data from the console you'll either need to teach people how to terminate the input or you'll need to use a different approach.
If you want to intercept any keypressed and react on them directly you may do so, too. You could, e.g., use ncurses and control your input via that. You could also set the concole to non-buffering (on POSIX systems using tcgetattr() and tcsetattr() to clear the ICANON flag) and deal directly with all key presses to decide whether you want to continue reading or not.
Although I'm certainly up to doing fancy I/O stuff I normally don't bother: users will understand the "end of input" character and just deal with it. That is, my input normally looks something like this:
while (in >> whatever_needs_to_be_read) { ... }
... or, if the input is genuinely line oriented
for (std::string line; std::getline(in, line); ) { ... }
The function doing this input will then be called with a suitable std::istream which may be std::cin although I have typically some way to also read from a file (in addition to the shell-privided input redirection).
BTW, despite some indications in the questions referenced, "EOF" is not a character being read. It is a character entered, though (normally). ... and it is quite conventional to "know" the end of input character (on POSIX systems a ctrl-D and on Windows a ctrl-Z). You can use other indicators, e.g., the "interrupt" (ctrl-C) but that takes more work and doesn't integrate nicely with stream. To use the interrupt chacter you'd need to setup a signal handler for SIGINT and deal with that. One slightly annoying part of doing so is that if you get it wrong you'll need to find a different way to kill the program (e.g. on POSIX using ctrl-Z to put the process to sleep and kill it via a harsher signal).

Examining a word in gdb prints in decimal instead of hex

I am trying to examine some addresses in gdb. It was printing in hex previously but I'm not sure how I changed it. When I enter x/20 $rsp the result looks like this:
0x7fffffffb060: -20336 32767 -559038737 0
Obviously this is not the end of the world since I can manually convert the values if needed but it is pretty annoying. I've tried exiting gdb and restarting but that does nothing.
gdb uses the last specified setting when printing values. To force hexadecimal, append x: x/20x addr.
faced the same issue, I try to print one byte first and then x/x shows hex values
or
use x/4bx to display 4 bytes in hex, there is an extra x in the end.

Regex match 1024KB convert to 1MB?

I get the file size from the index of the page, it's 1024KB and I want it to print 1MB stead of 1024KB, what should I do? (completely noob here)
I got this:
if($row[2]==1) // Rapidshare Check
{
$index=getpage($row[1]);
if(strpos($index,"FILE DOWNLOAD")===false) //check if page contains the word file download if not = bad link
{
mysql_query("UPDATE `v2links` SET `checked`='-1',`lastcheck`=NOW() WHERE `id`=".$row[0]);
print "bad link\n";
logstr("log-c.txt","bad link\n");
}
else
{
preg_match("**/([^\/\|\#\<\(\>\;\s][0-9]*[\s][KB]{2})/**",$index,$match);
$fsize=$match[1];
print $fsize."\n";
logstr("log-c.txt","bad link\n");
//logstr("log-c.txt","$caption | $fsize\n");
mysql_query("UPDATE `v2links` SET `checked`='1',`fsize`='$fsize',`lastcheck`=NOW() WHERE `id`=".$row[0]);
unset($match);
}
}
Thanks
How accurate do you want the conversion to be?
s/0*([0-9]+)[0-9]{3}K/\1M/g
will lop off the last three numbers of a four or greater digit size... but it's a truncation, and doesn't even consider math. (For example, 1999K -> 1M)
A possibly better method would be to s/K/000/g and s/M/000000/g to turn it into a (crude) number of bytes, and then convert as you want back down.
The best possible method would be to process it, if it has a M multiply by 1024*1024; if it has K multiply by 1024. (if it has G, multiply by 1024*1024*1024). Then, process the resulting size however you're trying to.
--Note that it'd be a good plan to store file size as an integer, rather than a string.
For processing and output, a series of if's are probably good enough, and you can set precise tollerances for how big something must be to be displayed as M instead of K.
--Or if you just want M, there's no if, and you just divide by 1024*1024.