C++ में cerr और clog
In this page:
std::cerr << "error message" << std::endl;
std::clog << "log message" << std::endl;
Standard Error Stream (cerr)
cerr standard error stream है, specifically normal program output की बजाय problems report करने के लिए बनाया गया।
खासकर, cerr unbuffered है, इसलिए इसमें लिखा हर message flush होने का इंतज़ार करने की बजाय तुरंत screen पर दिखता है — exactly वही जो आप एक crash या एक fatal error report करते समय चाहते हैं।
उदाहरण: Standard Error stream (cerr)
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
std::cerr << "Fatal error: file not found" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Log Stream (clog)
clog standard log stream है, diagnostic या status messages के लिए intended जो cerr के instant display की ज़रूरत जितने urgent नहीं हैं।
cerr के उलट, clog buffered है, मतलब यह output को memory में collect करता है और efficient batches में लिखता है, जो frequent logging के लिए बेहतर suited है जिसे आपके program को धीमा नहीं करना चाहिए।
उदाहरण: Log Stream (clog)
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
std::clog << "Status: request processed" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
cerr बनाम cout
cout और cerr दोनों terminal पर print करते हैं, लेकिन उन्हें अलग रखना आपको एक को दूसरे के बिना redirect करने देता है — उदाहरण के लिए, cout को एक results file में भेजना जबकि cerr फिर भी screen पर errors दिखाता रहे।
यही separation exactly वह वजह है कि command-line tools पहली जगह output को errors से अलग करते हैं।
उदाहरण: cerr vs cout
#include <iostream>
int main() {
std::cout << "Result: 42" << std::endl; // can be redirected separately
std::cerr << "Warning: low disk space" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
clog बनाम cerr
clog और cerr दोनों conceptually same error destination target करते हैं, लेकिन urgency और performance में अलग हैं: cerr तब इस्तेमाल करें जब एक message को होते ही तुरंत दिखना ज़रूरी हो, जैसे एक fatal error, और clog background diagnostic logging के लिए जहाँ buffering से एक छोटी delay बिल्कुल acceptable है।
उदाहरण: clog vs cerr
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
std::cerr << "Immediate: connection lost" << std::endl;
std::clog << "Background: cache refreshed" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Runtime Errors Handle करना
cout की बजाय cerr में errors लिखना guarantee करता है कि वे अभी भी visible रहें भले ही एक user आपके program का normal output myprogram > results.txt जैसी किसी चीज़ से एक file में redirect कर दे, क्योंकि वह redirection सिर्फ cout को affect करता है, cerr को नहीं।
यही वजह है कि well-behaved command-line programs कभी error messages को अपने cout output में मिलाते नहीं।
उदाहरण: Handling Runtime Errors
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "Normal output" << std::endl;
std::cerr << "Error output stays visible even if cout is redirected" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- error messages के लिए
coutइस्तेमाल करना, जो फिर normal output और redirections में मिल जाते हैं, जब errorscerrपर होने चाहिए। - यह उम्मीद करना कि
cerroutputcoutकी तरह buffered होगा, जब यह unbuffered है और तुरंत दिखता है। - यह मान लेना कि
cerrprogram रोक देता है, जब यह सिर्फ print करता है और program चलता रहता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: