470b4b4a9e1da73701e46abdc7d2da4adc7c2d87
[awesomized/libmemcached] / src / bin / common / checks.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "options.hpp"
19 #include "libmemcached/common.h"
20
21 #include <fstream>
22
23 bool check_buffering(const client_options &opt, memcached_st &memc) {
24 if (opt.isset("buffer")) {
25 if (MEMCACHED_SUCCESS != memcached_flush_buffers(&memc)) {
26 if (!opt.isset("quiet")) {
27 std::cerr << memcached_last_error_message(&memc) << "\n";
28 }
29 return false;
30 }
31 }
32 return true;
33 }
34
35 bool check_argp(const client_options &opt, char **argp, const char *error_msg) {
36 if (argp && *argp) {
37 return true;
38 }
39 if (!opt.isset("quiet")) {
40 std::cerr << error_msg << "\n";
41 }
42 return false;
43 }
44
45 bool check_memcached(const client_options &opt, memcached_st &memc) {
46 if (!memcached_create(&memc)) {
47 if (!opt.isset("quiet")) {
48 std::cerr << "Failed to initialize memcached client.\n";
49 }
50 return false;
51 }
52 return true;
53 }
54
55 bool check_return(const client_options &opt, memcached_st &memc, const char *key,
56 memcached_return_t rc) {
57 if (!memcached_success(rc)) {
58 if (!opt.isset("quiet")) {
59 if (MEMCACHED_NOTFOUND == rc) {
60 if (opt.isset("verbose")) {
61 std::cerr << "Could not find key '" << key
62 << "': " << memcached_strerror(&memc, rc) << "\n";;
63 }
64 } else {
65 std::cerr << "Fatal error for key '" << key
66 << "': " << memcached_last_error_message(&memc) << "\n";
67 }
68 }
69 return false;
70 }
71 return true;
72 }
73
74 std::ostream *check_ostream(const client_options &opt, const char *file, std::ofstream &stream) {
75 if (file && *file) {
76 if (opt.isset("debug")) {
77 std::cerr << "Opening '" << file << "' for writing.\n";
78 }
79 stream.open(file);
80 if (stream.is_open()) {
81 return &stream;
82 } else if (!opt.isset("quiet")) {
83 std::cerr << "Failed to open '" << file << "' for writing.\n";
84 }
85 }
86 return &std::cout;
87 }